Spring MVC的web.xml配置详解
spring是目前最流行的框架。创建java web项目时,我们首先会遇到的配置文件就是web.xml,这是javaweb为我们封装的逻辑,不在今天的研究中。下面我们将简单讲讲web.xml中的配置。
一、一个空的web.xml
1 1 <?xml version="1.0" encoding="UTF-8"?> 2 2 <web-app version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 4 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 5 id="WebApp_ID"> 6 6 </web-app>
二、标签介绍
web.xml中比较常见的标签以及其加载顺序为:
context-param > listener > filter > servlet
1、 <display-name>Archetype Created Web Application</display-name>
display-name 是标识项目的名称,这个不是很常用,可有可无的,或者说不需要我们去在意的东西。
2、 <context-param>
1 <context-param> 2 <param-name>webAppRootKey</param-name> 3 <param-value>60000</param-value> 4 </context-param>
context-param 是web.xml首先加载的标签,其下子标签有param-name和param-value.
此所设定的参数,在JSP网页中可以使用下列方法来取得:
1 ${initParam.webAppRootKey}
若在Servlet可以使用下列方法来获得:
1 String param_name=getServletContext().getInitParamter(“webAppRootKey”);
3、listener
1 <listener> 2 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 3 </listener>
listenter在项目开始的时候就注入进来,尽在context-param之后,所以正常我们将spring配置在listener 中,这样方法spring 初始化相关的bean。
4、filter
1 <filter> 2 <filter-name>CharacterEncodingFilter</filter-name> 3 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>UTF-8</param-value> 7 </init-param> 8 <init-param> 9 <param-name>forceEncoding</param-name> 10 <param-value>true</param-value> 11 </init-param> 12 </filter> 13 14 <filter-mapping> 15 <filter-name>CharacterEncodingFilter</filter-name> 16 <url-pattern>/*</url-pattern> 17 </filter-mapping>
filter起到一个过滤的作用,在servlet执行前后,像上面的配置就是在过滤servlet前将编码转换UTF-8,filter-mapping 则是将filter和url路径进行映射。其中init-param则是将初始化需要的参数传入到filter-class中从而进行初始化。filter和filter-mapping中的name必须是相同的,才能起到映射的作用,而filter-mapping 中的url-pattern则是匹配请求路径的。上面‘/*'表示过滤所有请求的servlet,如果写成‘/lzg',则过滤http://localhost:8080/项目名/lzg这个请求。
5、servlet
1 <servlet> 2 <!-- 配置DispatcherServlet --> 3 <servlet-name>springMvc</servlet-name> 4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 5 <!-- 指定spring mvc配置文件位置 不指定使用默认情况 --> 6 <init-param> 7 <param-name>contextConfigLocation</param-name> 8 <param-value>classpath:spring/spring-mvc.xml</param-value> 9 </init-param> 10 <!-- 设置启动顺序 --> 11 <load-on-startup>1</load-on-startup> 12 </servlet> 13 14 <!-- ServLet 匹配映射 --> 15 <servlet-mapping> 16 <servlet-name>springMvc</servlet-name> 17 <url-pattern>*.zxh</url-pattern> 18 </servlet-mapping>
servlet和filter类似,需要先指定servlet对应的class类,然后将这个类和utl路径请求地址进行映射。这里不多说了。
以上就是web.xml文件中出现最多的几个标签。其他的比如:
6、欢迎页
1 <welcome-file-list> 2 <welcome-file>login.jsp</welcome-file> 3 </welcome-file-list>
7、错误页
1 <!-- 后台程序异常错误跳转页面 --> 2 <error-page> 3 <exception-type>java.lang.Throwable</exception-type> 4 <location>/views/error.jsp</location> 5 </error-page> 6 7 <!-- 500跳转页面--> 8 <error-page> 9 <error-code>500</error-code> 10 <location>/views/500.jsp</location> 11 </error-page> 12 13 <!-- 404跳转页面 --> 14 <error-page> 15 <error-code>404</error-code> 16 <location>/views/404.jsp</location> 17 </error-page>