First Example of Spring MVC using Spring Boot

  • Spring team has given us web interface to quick start our project.

  • With help of this interface all dependencies will automatically added in pom.xml

  • Once we place that pom.xml in our project it downloads and will be placed in classpath.

Creating a Web application with Spring Initializr is a simple and very easy. We will use Spring Web MVC as our web framework.

Spring Initializr is great tool to bootstrap [initialize] your Spring Boot projects.

spring initializr

As shown in the image above, following steps have to be done:

  • Launch Spring Initializr and choose the following

    • Choose com.javabykiran as Group [ java package name]

    • Choose JBK_MVC_SpringBoot as Artifact [project name]

    • Choose following dependencies

      • Web

      • Actuator

      • DevTools

  • Click Generate Project.

  • Zip will be downloaded extract Zip and import project as shown below:

extract mvc boot file

  • Once extracted zip file, open eclipse and use import option.

import existing maven project in eclipse

  • Next -> Select Root folder as below:

select root folder for mvc spring boot

import maven projects in eclipse

  • Click on Finish.

  • Wait until all dependencies downloaded and check if complete project visible in navigator.

mvc spring boot project explorer

Spring Boot Starter Web provides all the dependencies and the auto configuration need to develop web applications. It is the first dependency we would use. No need to add below this is already added in pom.xml.

We want to use JSP as the view. Default embedded servlet container for Spring Boot Starter Web is tomcat. To enable support for JSP’s, we would need to add a dependency on tomcat-embed-jasper.
Add below in pom.xml

Following screenshot shows the different dependencies that are added in to our application because of Spring Boot Starter Web.

maven dependencies in spring boot starter web.

Dependencies can be classified into:

  • Spring - core, beans, context, aop

  • Web MVC - (Spring MVC)

  • Jackson - for JSON Binding

  • Validation - Hibernate Validator, Validation API

  • Embedded Servlet Container - Tomcat

  • Logging - logback, slf4j

Any typical web application would use all these dependencies. Spring Boot Starter Web comes prepackaged with these. As a developer, I would not need to worry about either these dependencies or their compatible versions.

Run main class This is very important class in spring boot application.

After running this class observe log on console. Read highlighted text once.

run main class of spring boot application

In summary below has happened, Spring Boot Starter Web auto-configures:

  • Dispatcher Servlet

  • Error Page

  • Web Jars to manage your static dependencies

  • Embedded Servlet Container - Tomcat is the default

We would have our jsp’s in /WEB-INF/jsp/. We would need to configure the view resolver with the prefix and suffix.

Open and add below lines there.

This file is very important all configuration level data can be placed here let’s say I want to run my server on port no 8090 add below line there:

  • public String showLoginPage(ModelMap model): Mapped to the \login Get Method, this method shows the login page.

  • @Autowired LoginService service: LoginService has the validation logic.

  • showWelcomePage(ModelMap model, @RequestParam String name, @RequestParam String password): Mapped to the \login Post Method, this method validates the userid and password. Redirects to welcome page if login is successful.

What is ModelMap,Map and ModevAndView? – We have covered this in Interview Questions Section. having business logic

JSP should be created in java\main\webapp\WEB-INF\jsp folder.

Simple login page with userid and password form fields. If error message is populated into model, ${errorMessage} will show the authentication failure error message.


Welcome page is shown on successful authentication. Shows the name of the login user and a link to manage to-do.


TO DO Service:

List todos pages shows the list of todo’s. This is completely unformatted page. During the subsequest steps of our course, we beautify this page and add more functionality to add, delete and update todo’s.

To-do Controller has a simple method to retrieve the list of to-do’s and populate it into model. It redirects to list-to-do’s view.

eclipse project structure

You can run this as a simple java application. You can launch the application at http://localhost:8080/login and enter userid/password combination of in jbk/jbk

run simple java application on localhost

run simple java application on localhost

run simple java application on localhost