一、
在本人的《web.xml加载顺序》文章中介绍到了,在web.xml中配置的加载顺序为:
ServletContext-> context-param ->listener -> filter -> servlet
本文就结合该加载顺序,详细的介绍一个较完整的工程的配置。
二、
该较完整的工程应该包括:
1、应用Spring;
2、应用SpringMVC;
3、实现SpringSecurity来实现权限管理和身份验证;
4、JVM加载工程时,实现配置信息初始化(读取xml文件、类文件的注解信息等)。
三、
完整的web.xml文件描述:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>securityTest</display-name> <description>security application test</description> <context-param> <param-name>projectCode</param-name> <param-value>securityTest</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml classpath:applicationContext-security.xml </param-value> </context-param> <filter> <filter-name>characterEncodingFilter</filter-name> <display-name>Character Encoding Filter</display-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> <!-- Use Spring's encoding filter --> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <!--filter-name>encodingFilter</filter-name--> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet> <servlet-name>security</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc-config.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>security</servlet-name> <url-pattern>*.s</url-pattern> </servlet-mapping> <servlet> <servlet-name>securityTestInitialServlet</servlet-name> <servlet-class> com.yc.securityTest.web.SecurityTestInitialServlet </servlet-class> <load-on-startup>10</load-on-startup> <init-param> <param-name>flag</param-name> <param-value>true</param-value> </init-param> </servlet> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
根据web.xml文件加载配置的顺序,可以把上面的配置详细描述为:
四、
对应于springMVC的搭建和配置可参考:
对应于springSecurity的搭建及配置可参考:
《springSecurity源码分析——DelegatingFilterProxy类的作用》
对应于实现配置信息初始化(读取xml文件、类文件的注解信息等),可参考我的另一篇文章《SpringMVC读取controller信息》。