Spring MVC beginner tutorial with Spring Tool Suite IDE
- Details
- Written by Nam Ha Minh
- Last Updated on 11 March 2020   |   Print Email
- Spring framework 3.2.2
- Spring Tool Suite IDE 3.2.0 (based on Eclipse Juno 4.2.2).
- vFabric tc Server Developer Edition v2.8 (based on Apache Tomcat and optimized for Spring applications).
1. About Spring Tool Suite IDE
2. A brief overview about Spring MVC
3. Creating a Spring MVC project in Spring Tool Suite IDE
3.1.Maven dependencies configuration
3.3.Web deployment descriptor (web.xml)
3.4.The example controller: HomeController.java
3.5.The example JSP view: home.jsp
4. Deploying and running the application
Watch this tutorial in video:1. About Spring Tool Suite IDE
Spring Tool Suite (STS) is an Eclipse-based IDE which is dedicated for developing Spring-based projects. It is actively developed and maintained by the SpringSource community. STS facilitates and simplifies Spring-based applications development by providing robust project templates such as Spring Batch, Spring Integration, Spring Persistence (Hibernate + JPA), Spring MVC, etc. In addition, with Maven integration, STS releases developers from manually managing Spring jar files in their projects. You always get the latest update of Spring artifacts from Maven repository.You can choose to download and install STS in one of three ways:- Download and install STS from an installer program.
- Install STS through Eclipse update.
- Download and extract zip archive.
2. A brief overview about Spring MVC
Let’s take a look at how Spring MVC works. The following diagram depicts the architecture of Spring MVC framework:
- Spring’s dispatcher servlet: acts as a front controller between the Spring application and its clients. The dispatcher servlet intercepts all requests coming to the application and consults the Handler Mapping for which controller to be invoked to handle the requests.
- Handler Mapping: is responsible to find appropriate controllers that handle specific requests. The mapping between request URLs and controller classes is done via XML configuration or annotations.
- Controller:is responsible to process the requests by calling other business/service classes. The output can be attached to model objects which will be sent to the view. To know which view will be rendered, the controller consults the View Resolver.
- View Resolver: finds the physical view files from the logical names.
- View:physical view files which can be JSP, HTML, XML, Velocity template, etc.
3. Creating a Spring MVC project in Spring Tool Suite IDE
Now let’s play with the Spring Tool Suite IDE to see how it leverages Spring application development.Start STS in your own workspace and make sure the current perspective is Spring (default). From main menu, select File > New > Spring Template Project:



- Project name: HelloSpringMVC
- Top-level package: net.codejava.springmvc





3.1. Maven dependencies configuration
Here’s a partial content of the pom.xml file:
http://localhost:port/springmvc
If we want to change version of Spring framework, just updating value of the org.springframework-version element. Because the version 3.1.1.RELEASE is not the latest, change it to the latest release 3.2.2.RELEASE (at writing time) as follows:<org.springframework-version>3.2.2.RELEASE</org.springframework-version>
Just by saving the pom.xml file, Maven will detect the change and updates all the related dependencies immediately.3.2. Spring MVC configuration
STS created two Spring configuration files: root-context.xml and servlet-context.xml.root-context.xml:

- <annotation-driven />: tells the framework to use annotations-based approach to scan files in the specified packages. Thus we can use the @Controller annotation for the controller class, instead of declaring XML elements.
- <resources mapping=…/>: maps static resources directly with HTTP GET requests. For example images, javascript, CSS,.. resources do not have to go through controllers.
- Bean InternalResourceViewResolver: this bean declaration tells the framework how to find physical JSP files according to logical view names returned by the controllers, by attaching the prefix and the suffix to a view name. For example, if a controller’s method returns “home” as logical view name, then the framework will find a physical file “home.jsp” under the /WEB-INF/views directory.
- <context:component-scan …/>: tell the framework which packages to be scanned when using annotation-based strategy. Here the framework will scan all classes under the package net.codejava.springmvc.
3.3. Web deployment descriptor (web.xml)
Here is content of the generated web.xml file:
3.4. The example controller: HomeController.java
Following is the generated code of the controller class:
model.addAttribute("serverTime", formattedDate);
And finally the method returns a view named “home”, which will be resolved by the view resolver specified in the servlet-context.xml file, to find the actual view file.
3.5. The example JSP view: home.jsp
The home.jsp file is generated under /WEB-INF/views directory with the following content:
The time on the server is ${serverTime}.
So far we have gone through all the stuffs generated by the Spring MVC Project template. Notice we haven’t written any line of code yet, but the application is ready to be deployed and tested now. Let’s go!4. Deploying and running the application
In Spring Tool Suite IDE, switch to the Servers view. Probably you should see the VMWare vFabric tc Server like this:
- Right click inside the Servers view then select New > Server from the context menu (Or click on the link new server wizard, if this link available):
- In the New Server dialog, select VMware > VMware vFabric tc Server…as follows:
- Click Next, you may have to select installation directory for the server:
For a STS installation, the server usually installed in the following directory:
STS_HOME\vfabric-tc-server-developer-VERSION
- Click Next. In the next screen, keep the option Create a new instanceselected:
- Click Next. In the next screen, type tcServer as name for the new instance and select base as the template:
- Click Finish, to complete the server setup and you should see the server appears in the Servers view.



http://localhost:8080/springmvc
If everything is going well (of course), we would see following screen:
5. Modifying the project
So far we have tested and seen the generated application running. Now let’s add some changes to the project for further understanding Spring MVC.Add the following method into the HomeController.java class:@RequestMapping(value = "/test", method = RequestMethod.GET) public String test(Model model) { String greetings = "Greetings, Spring MVC!"; model.addAttribute("message", greetings); return "test"; }This method will handle requests having the URL pattern /testand does the following chore:
- Adds a String object as an attribute into the model with name “message” and value is “Greetings, Spring MVC!”.
- Returns a logical view named “test”.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %> <html> <head> <title>Test page</title> </head> <body> <h1> ${message} </h1> </body> </html>This page is deadly simple, as it prints out value of the variable “message” which will be passed by the controller. Now get back to the browser window, change the URL to:
http://localhost:8080/springmvc/test
Hit Enter, we should be welcomed by the following screen:
Other Spring Tutorials:
- Spring Dependency Injection Example (XML)
- Understand the core of Spring framework
- Understand Spring MVC
- Understand Spring AOP
- Spring MVC Form Handling Tutorial
- Spring MVC Form Validation Tutorial
- 14 Tips for Writing Spring MVC Controller
- Spring Web MVC Security Basic Example (XML Configuration)
- Understand Spring Data JPA with Simple Example
About the Author:

Comments
Let try both the options to see which works.