Spring MVC Step By Step Example Tutorial

 

                            NOTE: Source Code available for download at the bottom of the page

 
 
What are we going to learn?
   We will develop a Spring Web MVC based web application and deploy it on tomcat server. To keep the project simple, we will just display the homepage. We will also examine what is required and how things work in Spring Web MVC.
 
What's needed?
  • Maven
  • JDK 1.5 or higher
  • eclipse 
  • Tomcat
Overall design
  • We will create a employee home page which displays the page title.
  • Load the Spring web application context through declaration in web.xml
 
Let's get started.
 
End Output:
 
Spring MVC Employee Project Home Page
Setup Eclipse Project
 
Create a dynamic web project using maven artifact "maven-archetype-webapp" as shown HERE
 
 
Following Steps summarize the process for creating the Spring MVC project.
  1. Update POM file to get dependencies for Spring Web MVC
  2. Add declaration for Spring DispatcherServler servlet in web.xml.
  3. Create Web Application Context file.
  4. Create Controller
  5. Create JSP Page
 
Step 1: Update POM file to get dependencies for Spring Web MVC
 
For Spring Web MVC jars 
 
pom.xml
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
	<version>4.0.2.RELEASE</version>
</dependency>
 
 
Step 2: Add declaration for Spring DispatcherServler servlet in web.xml.
 
web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Employee Project SPRING WEB MVC</display-name>
  
  <servlet>
   <servlet-name>Employee</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
   	<param-name>contextConfigLocation</param-name>
    	<param-value>
        	 /WEB-INF/employee-servlet.xml
        </param-value>
   </init-param>
</servlet>

<servlet-mapping>
   <servlet-name>Employee</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>
  
</web-app>
 
 
<servlet-mapping> for Employee forwards all the requested url's to servlet named Employee

<servlet> declaration for Employee is defined as Spring DispatcherServlet. Upon initialization of a DispatcherServlet, 
Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and createsthe beans defined there, overriding the definitions of any beans defined with the same name in the global scope.If you want to name the context file with name different than [servlet-name]-servlet.xml , you can use  
		
		<init-param>
		
		<param-name>contextConfigLocation</param-name>
		
		<param-value>
		
		/WEB-INF/employee-servlet.xml
		
		</param-value>
		
		</init-param>
			
 to define the location and file name.
 
 
Step 3: Create Web Application Context file.
 
employee-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:annotation-config />
	<context:component-scan base-package="com.visionjava" />
	<mvc:annotation-driven />

	<bean id="viewResolver"	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="cache" value="true" />
		<property name="prefix" value="/WEB-INF/jspPages/" />
		<property name="suffix" value=".jsp" />
		<property name="redirectHttp10Compatible" value="false" />
	</bean>

</beans>
 
 
<context:annotation-config /> is used to activate annotations in beans already registered in the application context. Ex: @Autowired, @Resource, @Required, @PostConstruct

									
<context:component-scan base-package="com.visionjava" /> scans the defined packages and registers the beans with application context(like controller, bean).

<mvc:annotation-driven /> declares explicit support for new Spring MVC features like @Valid, @RequestBody, @RequestResponse etc.


<bean id="viewResolver"	class="org.springframework.web.servlet.view.InternalResourceViewResolver"> provides access to controller for the jsp pages under WEB-INF folder. It's a good practice to put all the jsp pages under WEB-INF folder to avoid giving direct access.
 
Step 4: Create Controller
 
 
 
package com.visionjava.employee.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class EmployeeController {

	@RequestMapping("/")
	public String showHomePage(){		
		return "employeeHomePage";
	}
	
}
 
@Controller -- declares the class as Spring Controller type class.

@RequestMapping("/")-- Annotation for mapping web requests onto specific handler classes and/or handler methods.In our case the homepage request is handled by this controller.
The return value is resolved by the view resolver which appends the location and suffix for the string.

									
Step 5: Create JSP Page
 
employeeHomePage.jsp
<html>
<body>
<h2>Employee Home Page!</h2>
</body>
</html>
 
 
 
 
 
Project Structure:
 
 
Spring MVC Employee Project Structure
 
 
 
 
Source Code : Employee.rar
Web Analytics