Fork me on GitHub

SSM整合三———整合SpringMVC

6.整合SpringMVC

6.1导入jar包

6.2配置web.xml

注意:由于我用的spring版本是5.1.2,所以jackson的版本应高于2.4.2,否则会出现版本冲突。

  1. 通过web.xml来启动spring,加载applicationContext.xml
  2. 通过web.xml来启动spring,加载spring-mvc.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
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>06.mybatis-spring-springmvc</display-name>

<!-- 404错误拦截 -->
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
<!-- 500错误拦截 -->
<error-page>
<error-code>500</error-code>
<location>/error500.jsp</location>
</error-page>


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
<!-- <param-value>WEB-INF/classes/spring/applicationContext-*.xml</param-value> -->
</context-param>

<!-- 配置前端控制器 启动SpringMVC-->
<servlet>
<servlet-name>DispatcherServle</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- ContextconfigLocation配置springmvc加载的配置文件
适配器、处理映射器等
-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServle</servlet-name>
<!-- 1、.action访问以.action结尾的 由DispatcherServlet进行解析
2、/,所有访问都由DispatcherServlet进行解析
-->
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- 解决post乱码问题的过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>



</web-app>
6.3配置spring-mvc.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
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描controller包下所有标注@Controller的组件 -->
<context:component-scan base-package="net.zzqd.ssm.controller" />
<!-- mvc注解驱动 -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 浅醉 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- 静态资源 解析 -->
<mvc:resources location="/script/" mapping="/script/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>

</beans>
6.3编写Controller

过程:通过web.xml中Listener和servlet加载spring容器以及启动springMVC,来读取applicationContext.xml文件和spring-mvc.xml文件,然后spring-mvc中启动mvc注解,以及视图解析器和Controller扫描

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package net.zzqd.ssm.controller;

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

@Controller
@RequestMapping("/customer")
public class CustomerController {

@RequestMapping("/test")
public String test()
{
return "test";
}
}
6.4编写页面
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>Insert title here</title>
</head>
<body>
<h1>测试springmvc是否可用</h1>
</body>
</html>
6.5访问结果