前面做了几篇有关于springmvc+mybatis的小工程,前面的博客中只有源码没有分析,后面这几篇专门来分析下各种xml和代码。这篇主要来分析下web工程中web.xml的作用及配置。
一、web.xml文件相当于web工程的启动引导文件,web.xml中的配置信息提示web容器在启动此web工程时需要加载的内容以及加载顺序。web.xml中配置的加载顺序为context-param->listener->filter->servlet。
1、web容器在启动时会按照上面的顺序来加载资源,所以上述节点在文件中的实际位置是不会影响容器的加载顺序的。
2、在某些类型之间上下顺序是不能乱的,比如<filter>和<filter-mapping>,<servlet>和<servlet-mapping>,如果<filter-mapping>出现在<filter>之上,就会出现filter-name未定义抛出异常。servlet和servlet-mapping也是一样的。
二、下面就对照以下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
43
44
45
46
47
48
49
50
51
52
53
54<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!-- 修改spring配置文件applicationContext.xml默认配置,下面的配置将文件的默认位置WEB-INF/applicationContext.xml下的路径修改为src/applicationContext.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 配置ContextLoaderListener表示,该工程要以spring的方式启动。启动时会默认在/WEB-INF目录下查找applicationContext.xml 作为spring容器的配置文件,该文件里可以初始化一些bean --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 字符集过滤器 --> <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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring mvc 配置 --> <!-- 配置DispatcherServlet表示,该工程将采用springmvc的方式。启动时也会默认在/WEB-INF目录下查找XXX-servlet.xml作为配置文件, XXX就是DispatcherServlet的名字,该文件中将配置两项重要的mvc特性:HandlerMapping,负责为DispatcherServlet这个前端控制器的请求查找Controller; ViewResolver,负责为DispatcherServlet查找ModelAndView的视图解析器。 此处使用指定的配置文件spring-mvc.xml --> <servlet> <servlet-name>contacts</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--<param-value>/WEB-INF/classes/spring-mvc-servlet.xml</param-value> --> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>contacts</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
1<?xml version="1.0" encoding="UTF-8"?>
xml文件头,及输出编码格式
1
2
3
4<web-app version="2.5" 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_2_5.xsd">
这个声明告诉容器servlet的版本
1
2
3
4<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
这个配置重新指定了spring配置文件applicationContext.xml的位置,spring默认会在/WEB-INF下查找applicationContext.xml文件,这里重新定义后,就会在class下查找applicationContext.xml,这时需要将文件放到src下即可。
这里简单介绍下classpath作用
classpath的作用为:在class下寻找applicationContext.xml,还有一种为classpath*的作用为:不仅包含class路径,还包括jar文件中(class路径)进行查找。
1
2
3<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
ContextLoaderListener类当容器启动时,加载applicationContext.xml,默认路径为/WEB-INF/applicationContext.xml,当然和这个是可以修改的,我们这里通过上一步contextConfigLocation这个配置已经将默认路径修改为src/applicationContext.xml。这个配置是使用spring时的必须配置。
1
2
3
4
5
6
7
8
9
10
11
12<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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
这个是配置字符集过滤器,这个配置的作用是解决我们的java文件和jsp文件中字符集不一样时可能出现的乱码情况。这里在简单的说明下<url-pattern>/*</url-pattern>中url的配置,
/ 会匹配到像/login的数据,但是匹配不到像*.jsp这种带后缀的,/*会匹配到所有url
1
2
3
4
5
6
7
8
9
10
11
12
13
14<servlet> <servlet-name>contacts</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--<param-value>/WEB-INF/classes/spring-mvc-servlet.xml</param-value> --> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>contacts</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
这个就是前面工程中所使用的web.xml文件,这里对配置坐下做下解读
最后
以上就是清脆小伙最近收集整理的关于web.xml配置详解的全部内容,更多相关web内容请搜索靠谱客的其他文章。
发表评论 取消回复