Web-xml配置文件
# Web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--1、配置Spring文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2、配置DispatcherServlet,加载SpringMVC的核心(请求分发器/前端控制器)-->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--配置DispatcherServlet要绑定Spring的配置文件-->
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!--启动级别1:和服务器一起启动-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 2.1将所有请求映射到 DispatcherServlet 进行处理 -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!--2.2配置url-pattern 路径
/:只匹配所有的请求,不会匹配jsp等页面
/*:匹配所有的请求,包括匹配jsp等页面-->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--3、Post请求过滤器-->
<!--spring提供的characterEncodingFilter配置 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<!--观察发现这个类 CharacterEncodingFilter 有一个属性 encoding 所以提供一个initparm以及 value -->
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--给CharacterEncodingFilter类的对象进行初始化的赋值request.setCharacterEncoding-->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!--响应编码的设置 true 设置response -->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!--3.1 过滤器对哪些资源进行过滤呀 -->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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
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
编辑 (opens new window)