springmvc里的web。xml详解

来源:互联网 发布:淘宝钻石店铺好做吗 编辑:程序博客网 时间:2024/06/09 21:41
web.xml里加载顺序:context-param>listener>filter>servlet

首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关。即不会因为 filter

写在 listener的前面而会先加载 filter。最终得出的结论是:

ServletContext-> listener ->filter -> servlet

      同时还存在着这样一种配置节:context-param,它用于向 ServletContext提供键值对,

即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,

那么context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param配置节可写在任意位置,因此真正的加载顺序为:context-param -> listener-> filter -> servlet

       对于某类配置节而言,与它们出现的顺序是有关的。以filter 为例,web.xml 中当然可以定义多个 filter,与 filter 相关的一个配置节是filter-mapping,这里一定要注意,对于拥有相同 filter-name 的 filter 和 filter-mapping配置节而言,filter-mapping 必须出现在 filter 之后,否则当解析到 filter-mapping 时,它所对应的filter-name 还未定义。web 容器启动时初始化每个 filter 时,是按照 filter配置节出现的顺序来初始化的,当请求资源匹配多个 filter-mapping 时,filter 拦截资源是按照filter-mapping 配置节出现的顺序来依次调用 doFilter() 方法的。

       servlet 同 filter类似,此处不再赘述。

 

     由此,可以看出,web.xml 的加载顺序是:ServletContext-> context-param ->listener -> filter -> servlet,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0" metadata-complete="true"> <display-name>jwpt</display-name> //log4jConfigLocation这个contextparam与下面的listener(1)对应,并且在jar包源码中能查到该属性  <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param>
//log4jRefreshInterval这个contextparam与下面的listener(1)对应,并且在jar包源码中能查到该属性,  与上面的一样都是log4j的东西
 <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param>
//contextConfigLocation这个contextparam与下面的listener(2)对应,是加载springmvc的bean的load的   配置文件,这样springmvc在xml里配置的bean节点才能映射到java类里
 <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:com/tykj/resources/applicationContext.xml </param-value> </context-param> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter> <description>登录验证过滤</description> <filter-name>loginCheckFilter</filter-name> <filter-class>com.tykj.djksk.core.filter.CheckLoginFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>loginCheckFilter</filter-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.do</url-pattern> </filter-mapping>

(1) <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> (2)<listener> <description>被包装的Spring监听器继承ContextLoaderListener</description> <listener-class>com.tykj.djksk.core.context.StartUpListener</listener-class> </listener>

//(3),(4)这两个listener完全是为了web应用来设置的,他俩没有contextparam来与之对应,但是也很重要
 (3)<listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> (4)<listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener>


//servlet(1)是为了flex的前台与service的数据交互,这在使用flex的时候是必须的
(1) <servlet> <servlet-name>MessagebrokerServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/tykj/resources/applicationContext-flex.xml</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MessagebrokerServlet</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping>
//servlet(2)是springmvc的初始化配置,这里配置了:默认扫描、返回字符串的前缀后缀、方法传递的格式、如果是xml配置的话,这里面需要配置的更多。<load-on-startup>1</load-on-startup>这个配置是如果是0或者大于0则是在web容器加载的时候就加载,如果小于0是serverlet选择时才加载,数越小优先级越高

(2) <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/tykj/resources/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <session-config> <session-timeout>-1</session-timeout> </session-config> <welcome-file-list> <welcome-file>/logindjk.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config></web-app>
0 0