Introduction to Spring MVC - bahkified/Notes GitHub Wiki
This short guide will show the basic steps that need to be taken in order to use Spring MVC. This assumes that you have already created a web project with Maven.
pom.xml
In the POM, include the appropriate dependencies. The following includes extra dependencies that may be used later for auto wiring in Spring, unit testing, logging, etc.
<properties>
<spring.version>4.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Javax dependencies -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Logging dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<!-- Testing dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies>
web.xml
In order to use Spring MVC, traffic must be routed through Springs DispatcherServlet, which sends requests to the appropriate controllers. Your deployment descriptor should include something that looks like the following:
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>fun</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fun</servlet-name>
<url-pattern>/fun/*</url-pattern>
</servlet-mapping>
</web-app>
The servlet name in web.xml
is automatically mapped to a spring configuration file called <servlet-name>-servlet.xml
. At a minimum, this file needs to include your controller classes and a view resolver.
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.bahk.fun.controller"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
This configuration file includes one package to be scanned and has a JSP view resolver, which looks for the JSP views in /WEB-INF/views/
directory by name.
In this example, JSPs are used. The following is a test page that can be viewed to ensure that Spring MVC is working. It is named:
test.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Fun Test</title>
</head>
<body>
<div class="container">
<h1>This is a test.</h1>
</div>
</body>
</html>
There are two important annotations that must be used in Spring MVC. They can both be seen in this very simple controller.
FunController.java
@Controller
public class FunController {
private Logger logger = LoggerFactory.getLogger(getClass());
@RequestMapping("test")
public String doTest() {
logger.debug("Entering test!");
return "test";
}
}
This controller will resolve a single URL: /fun/test
It will return a the String, "test", which will be picked up by the view resolver. The view resolver will look for test.jsp and display it.
@Controller
annotation tells Spring MVC that this class is a controller class. The package in which this class lives must be included in the <context:component-scan base-package="…"/>
element in the Spring config file, otherwise, Spring will not know that it exists!
@RequestMapping("test")
annotation tells Spring MVC that any request that ends with <servlet-name>/test
will be handled by this particular method.
You have now set up a basic Spring MVC web app. This app can be packaged into a WAR and deployed onto a server. It should work as is, though only a single URL will be functional (.../test)!