Spring MVC Tutorial – View Pages

In this Spring MVC Tutorial, we will learn how to develop Spring MVC web application using Spring Tool Suite. Spring MVC framework is widely used for java web applications.

Spring MVC

Just like Struts Framework, Spring MVC is also based on Java EE Servlet and JSP technologies and implement ModelViewController design pattern.

Spring MVC Tutorial

We have earlier seen how Spring Dependency Injection works and in this tutorial we will learn how to create a simple web application using Spring MVC framework.

We can use Eclipse or IntelliJ IDE for the Spring projects development, but SpringSource provides Spring Tool Suite (STS) that is an IDE based on Eclipse and comes with in-built VMware vFabric tc Server that is built on top of Apache Tomcat container and optimised for Spring-based applications.

I would use STS for Spring MVC tutorial and other future tutorials because it makes a developer life easier by providing the following features:

  • Support for creating skeletal Spring applications (MVC, Rest, Batch etc), good for starting the project from scratch. We will soon see in this spring MVC tutorial how easy it is to create a Spring MVC project.
  • Provides useful features such as creating Spring Configuration files, parsing config files and classes to provide useful information about them.
  • Automatic validation of Spring application
  • Refactoring support to easily make project changes, the changes get reflected in config files too.
  • Code assist for not only classes but configuration files too, I like this feature a lot because most of the times we need to know what we can use and its details.
  • Best support for Aspect Oriented Programming (AOP) through the integration of AspectJ.

Looking at all the features STS provide, I was sold for this and decided to use it for Spring application and till now I am very happy with it.

Just Download the STS from STS Official Download Page and install it. I am using STS 3.4.0.RELEASE that is based on Eclipse 4.3.1 release.


If you don’t want to use STS and want to get its facilities in existing Eclipse, then you need to install its plugin from Eclipse Marketplace. Use below image as a reference and make sure to chose the correct STS version for installation. Below plugin is good for Eclipse Kepler.

If you don’t want to use SpringSource server, you can deploy the application in any other Java EE container such as Tomcat, JBoss etc. For this tutorial, I will use the server that ships with STS but I have tested the application by exporting it as WAR file into separate tomcat server and it’s working fine.

Now that our server environment and IDE is ready, let’s proceed to create our first Spring MVC project. Below steps are valid for STS as well as Eclipse with STS plugins.

Creating Spring MVC Application in STS or Eclipse

Step 1: Create New Spring Project from the menu.

Step 2: In the new project window, give the name as “SpringMVCExample” and chose template as “Spring MVC Project”. If you are using this template for the first time, STS will download it from SpringSource website. If you want, you can add the project to any working set.

Spring MVC

Spring MVC Template Download, Spring MVC Project, Spring MVC Example

Step 3: When the template is downloaded, in the next screen you need to provide the top-level package name. This package will be used as the base-package for Spring components.

Spring MVC source Package, Spring MVC, Spring MVC Example, Spring MVC Tutorial

Step 4: Once the project is created by Spring MVC template, it will look like below image.

Spring MVC

Don’t worry if you don’t see User.java class, login.jsp and user.jsp files, they have been added by me later on.

If your project is not compiled and you see some errors, run Maven/Update Project. Make sure to check the “Force update of Snapshots/Releases” options, refer below image.

Spring MVC, Maven Force update projects

Overall project looks just like any other maven based web application with some Spring configuration files. Now it’s time to analyze the different part of the projects and extend it a little.

Spring MVC Dependencies

Our generated pom.xml file looks like below.

artifactId will be the servlet-context for the web application, so you can change it if you want something else.

There are few properties defined for Spring Framework, AspectJ and SLF4j versions, I found that they were not reflecting the latest versions, so I have changed them to the latest stable version as of today.

The project dependencies that I am interested in are;

  • spring-context: Spring Core dependency. Notice the exclusion of commons logging in favor of SLF4J.
  • spring-webmvc: Spring artifact for MVC support
  • aspectjrt: AspectJ API reference
  • SLF4J and Log4j: For logging purposes, Spring is very easy to configure for log4j or Java Logging API because of SLF4J integration.
  • javax.inject – JSR330 API for dependency injection

There are some other dependencies added, such as Servlet, JSP, JSTL and JUnit API but for starter application, we can overlook them.

Spring MVC Tutorial – Log4j Configuration

The generated log4j.xml file looks like below.

Notice that it’s printing everything to console, we can easily add appenders to redirect logging to files.

Spring MVC Tutorial – Deployment Descriptor Configuration

Let’s see our web.xml and analyze it.

ContextLoaderListener ties the ApplicationContext lifecycle to ServletContext lifecycle and automate the creation of ApplicationContext. ApplicationContext is the place for Spring beans and we can provide it’s configuration through contextConfigLocation context parameter. root-context.xml file provides the configuration details for WebApplicationContext.

DispatcherServlet is the controller class for Spring MVC application and all the client requests are getting handled by this servlet. The configuration is being loaded from the servlet-context.xml file.

Spring MVC Tutorial – Configuration Files

root-context.xml file:

We can define shared beans here, as of now there is nothing in it.

servlet-context.xml code:

This is how the standard Spring configuration file looks like, just imagine writing all this on your own and you will start liking STS tool.

annotation-driven element is used to let Controller servlet know that annotations will be used for bean configurations.

resources element defines the location where we can put static files such as images, HTML pages etc that we don’t want to get through Spring framework.

InternalResourceViewResolver is the view resolver, we can provide view pages location through prefix and suffix properties. So all our JSP pages should be in /WEB-INF/views/ directory.

context:component-scan element is used to provide the base-package location for scanning Controller classes. Remember the value of the top-level package given at the time of project creation, it’s the same value getting used here.

Spring MVC Controller Class

HomeController is created automatically with the home() method, although I have extended it a little bit by adding loginPage() and login() methods.

@Controller annotation is used to indicate that it’s a web controller class.

@RequestMapping is used with classes and methods to redirect the client request to specific handler method. Notice that handler methods are returning String, this should be the name of view page to be used as the response.

As you can see that we are having three methods returning different strings, so we need to create JSP pages with the same name.

Notice that login() method will get called with HTTP method as POST, so we are expecting some form data here. So we have User model class and it’s marked for validation using @Validated annotation.

Every method contains Model as an argument and we can set attributes to be later used in the JSP response pages.

Spring MVC Model Classes

Model classes are used to hold form variables, our User model bean looks like below.

A simple java bean with the variable name and its getter and setter methods.

Spring MVC Tutorial – View Pages

We have three JSP pages like below.

home.jsp code:

Notice the use of JSP Expression Language to get the attribute values.

login.jsp code:

A simple JSP page for the user to provide the userName as input. Notice that form variable name is same as User class variable name. Also, form action is “home” and method is “post”. It’s clear that HomeController login() method will handle this request.

user.jsp code:

Simple home page for the user where username is displayed, notice that we are setting this attribute in the login method.

Spring MVC Example Application Testing

Our application is ready for execution, just run it on the VMware tc Server or your choice of any other servlet container, you will get below pages as the response.

Spring MVC Hello World, Spring MVC, Spring MVC Example

Spring MVC Hello World, Spring MVC

Spring MVC Hello World, Spring MVC Tutorial

That’s it for Spring MVC Tutorial, you can see that how easy it is to create Spring MVC application using STS plugins. The code size is very less and most of the configuration is handled by Spring MVC so that we can focus on business logic. Download the example spring MVC project from below link and play around with it.

By admin

Leave a Reply