How Spring MVC works
Basically the same way as Struts:
-
Based on the HTTP request URL, the DispatcherServlet calls the corresponding Controller.
-
A View is rendered and sent as HTTP response.
Spring Servlet Declaration
In 'WEB-INF/web.xml', we declare Spring DispatcherServlet and map '*.html' URLs to it:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> jsp/index.jsp </welcome-file> </welcome-file-list> </web-app>
Spring Servlet Configuration
Let's now create the Spring configuration file 'WEB-INF/springmvc-servlet.xml' (name based on the servlet name above):
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean name="/hello_world.html" class="springmvc.web.HelloWorldController"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
-
Map URL /hello_world.html to Controller HelloWorldController
-
Declare View Resolver: when view ' view_name' is called (from the Controller), the file ' /jsp/view_name.jsp' will be used.
Controller
Let's create the Controller 'WEB-INF/src/springmvc/web/HelloWorldController.java':
package springmvc.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloWorldController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String aMessage = "Hello World MVC!";
ModelAndView modelAndView = new ModelAndView("hello_world");
modelAndView.addObject("message", aMessage);
return modelAndView;
}
}
This Controller calls the view 'hello_world', passing 'message' to it (like a Struts attribute).
View
Now the view: 'jsp/hello_world.jsp':
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<p>This is my message: ${message}</p>
</body>
</html>
Displays the message attribute previously set in the Controller.
Make it work
From 'WEB-INF' folder type
ant
Relaunch Tomcat and check it's working:http://localhost:8180/springmvc/hello_world.html
Summary
Your project should now look like that:
You can download it here. With Java 6, delete the class files (ant clean
) before compiling (ant
).
-
We declared Spring servlet in web.xml
-
We created a Spring configuration file where we mapped an URL to a controller and defined a way to resolve views.
-
We created a controller and a view