Spring MVC Tutorial Step by Step for Beginners

This tutorial introduces you to Spring MVC Tutorial Step by Step for Beginners, using the Spring MVC example with full xml configuration. Spring MVC 4 is a main part of Spring Frameworks and is great for you to build web applcation.

It is simple if you are familiar with creating a maven project on eclipse. The project should be created such as the below structure.

Other interesting posts you may like

The Spring MVC Tutorial Step by Step for Beginners guides all steps like below:

Project structure

Spring MVC Tutorial Step by Step for Beginners

Maven Dependency
Now, you should put all the project dependencies into file pom.xml and maven will take care of downloading all the that dependencies automatically. Here is the our file pom.xml.

Add Controller and View
Then create a FirstController class such as the code snippet

Lets look into the above code snippet

The @Controller annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the Servlet API. However, you can still reference Servlet-specific features if you need to.

The @RequestMapping annotation to map URLs such as “/” onto an entire class or a particular handler method. In our case, we have applied it on class level too, which says that this class is default handler for all HTTP requests of type ‘/’. @RequestMapping have several attributes [value,method,params,..] which can be used to narrow down the mapping to more specific selection.

ModelMap is implementation of Map for use to pass multiple values from Spring MVC controller to JSP

One more point that you should focus on the return values of these methods. These values will be suffixed and prefixed with suffix and prefix defined in view resolver to form the real view file name. Lets see spring-servlet.xml and create a file view is with name display.jsp under the directory /WEB-INF/views/. You will have the full path of file view is WEB-INF/views/display.jsp.

Create Spring configuration file
Spring configuration file contains bean definitions and other context information for Spring framework. Here we are using XML configuration but we could use Annotation based Configuration also. This tutorial will be focus on Xml configuration and the Annotation will be topic for next tutorial.

The spring-servlet.xml file will be created with below content in WEB-INF folder. The name file is anything you like but be sure to declare it in web.xml.

Main points in the spring-servlet.xml file

mvc:annotation-driven is used for enabling the Spring MVC components with its default configurations and help to define spring beans dependencies without using xml also.

The above declaration in the spring application configuration file would scan the classes inside the specified package and create the beans instance. Note that it could create beans only if that class is annotated with correct annotations

context:component-scan base-package=”com.javabycode.springmvc” tells spring to scan the classes inside the specified package and create the beans instance, and look at each class to see if it is annotated with specific Spring annotations [@Controller, @Service, @Repository, @Component, etc..] then Spring will register it with the bean factory.

View resolver(InternalResourceViewResolver) will help controller to delegate the response to corresponding view.

Configure web configuration

Things to take from here are DispatcherServlet & contextConfigLocation. DispatcherServlet is the front controller which receives each request(see the url pattern) and direct the request to appropriate controller.It is also responsible for directing the response from controller to appropriate views.

Look carefully at contextConfigLocation init-param. Thanks to this parameter, you can name your spring configuration file anything you want and place anywhere you want, even you can provide multiple files. In absence of this parameter, you are obliged to name the file as ABC-servlet.xml where ABC is the dispatcher servlet name.

Deploy spring application

Open browser and run with the address http://localhost:8080/Spring4MVCFirstExample

spring mvc example - Spring MVC 4 Tutorial deploy

Run with the address http://localhost:8080/Spring4MVCFirstExample/introduce

spring mvc example - Spring MVC 4 Tutorial deploy

That’s it. This is the first Spring MVC Example in the Spring MVC Tutorial Step by Step for Beginners series. We will post the next tutorial, please visit usually blog to read and share your opinion.

Download complete source code of example, please click link below

Spring4MVCFirstExample.zip (69 downloads)

Source coden on Github https://github.com/javabycode/spring-mvc-tutorial-step-by-step-for-beginners

Leave a Comment


Please share it if you found this useful
Hide Buttons