搭建 Spring MVC 框架,上接《基于 Maven 的 Web 项目搭建》
引入 Spring MVC 包
在 Maven 的pom.xml
中,添加如下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| <properties> <springVersion>4.3.7.RELEASE</springVersion> </properties>
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>compile</scope> </dependency>
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springVersion}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springVersion}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springVersion}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${springVersion}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${springVersion}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springVersion}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${springVersion}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${springVersion}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springVersion}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${springVersion}</version> <scope>compile</scope> </dependency> </dependencies>
|
配置 Web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Simple Demo</display-name>
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
<filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
</web-app>
|
新建目录
/WEB-INF/
下,新建 assets
作为静态资源目录
/WEB-INF/
下,新建 views
作为页面目录
src/main/resources/
,新建 config/spring
作为 Spring
的配置文件目录
配置 Servlet
在 src/main/resources/config/spring
中,新建 spring-servlet.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<mvc:resources mapping="/assets/**" location="/WEB-INF/assets/" />
<mvc:annotation-driven />
<context:component-scan base-package="xyz.demo.*"></context:component-scan>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
|
测试
通过写一个首页访问,测试是否配置成功
src/main/java
,新建包xyz.demo.home.controller
,新建类HomeController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package xyz.demo.home.controller;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;
@Controller @RequestMapping(value = "/home") public class HomeController {
@RequestMapping(value = "/index", method = RequestMethod.GET) public String hello(Model model) { model.addAttribute("msg", "This is Spring MVC"); return "index"; }
}
|
src/main/webapp/WEB-INF/views
,新建文件index.jsp
1 2 3 4 5 6 7 8 9 10 11 12
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>hello</title> </head> <body> <p>${ msg }</p> </body> </html>
|
- 启动 Tomcat 服务,访问
http://localhost:8080/simpleDemo/home/index
后记
请求处理流程简单描述
- 请求
http://localhost:8080/simpleDemo/home/index
- 被
web.xml
中配置的org.springframework.web.servlet.DispatcherServlet
拦截
- 解析请求地址
home/index
- 通过
HandlerMapping
找到HomeController
类的映射(@RequestMapping(value = "/home")
),再找到home
方法的映射(@RequestMapping(value = "/index", method = RequestMethod.GET)
)
- 调用
home
方法后,根据返回类型(String
)和返回值(return "index";
),通过spring-servlet.xml
中配置的org.springframework.web.servlet.view.InternalResourceViewResolver
,在/WEB-INF/views/
中,找到index.jsp
,渲染视图
- 返回给浏览器
Spring MVC 最少引入包
1 2 3 4 5 6 7 8
| spring-web-4.x.x.RELEASE.jar spring-beans-4.x.x.RELEASE.jar spring-context-4.x.x.RELEASE.jar spring-core-4.x.x.RELEASE.jar commons-logging-1.1.3.jar spring-webmvc-4.x.x.RELEASE.jar spring-aop-4.x.x.RELEASE.jar spring-expression-4.x.x.RELEASE.jar
|
参考资料