Web.xml

初始化过程

  1. 在启动Web项目时,容器(Tomcat)会读web.xml中的两个节点和。
   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/spring-beans.xml</param-value>
    </context-param>
    <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  1. 容器会创建一个ServletContext(上下文), 作为servlet和容器之间的桥梁,整个WEB项目都能使用这个上下文。
  2. 容器会将读取到转化为键值对,并交给上下文。
  3. 容器根据加载spring的监听器实例ContextLoaderListener, 在监听到ServletContext上下文改变的时候,运行监听器的contextInitialized(ServletContextEvent event)方法,开始初始化Spring的上下文实例WebApplicationContext。
  4. 同时在这个方法中通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。
  5. spring加载器会取key=’contextConfigLocation’, 的参数,然后去加载咱们配置的各种组件(spring data,数据库配置等等)。
  6. 加载过滤器filter和servlet
    由上面的初始化过程可知容器对于web.xml的加载过程是context-param >> listener >> filter >> servlet

配置源码

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
   version="2.5">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/spring-beans.xml</param-value>
    </context-param>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<!-- 
    设置委托过滤器代理,让spring管理filter的生命周期等
    参考链接`http://blog.csdn.net/geloin/article/details/7441937`
-->
<!-- 
    <filter>
        <filter-name>springSessionRepositoryFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>redisSessionFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
-->

    <!--编码过滤器,设置request的编码格式-->
    <filter>
        <filter-name>springEncoding</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>springEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--验证码-->
    <servlet>
        <servlet-name>StickyCaptcha</servlet-name>
        <servlet-class>cn.apiclub.captcha.servlet.StickyCaptchaServlet</servlet-class>
        <!-- 宽度设置和高度设置是可选的,如果没有指定默认的为宽200px,高50-->
        <init-param>
            <param-name>width</param-name>
            <param-value>250</param-value>
        </init-param>
        <init-param>
            <param-name>height</param-name>
            <param-value>75</param-value>
        </init-param> 
        <init-param>
            <param-name>ttl</param-name>
            <param-value>0</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>StickyCaptcha</servlet-name>
        <url-pattern>/captcha.leaf</url-pattern>
    </servlet-mapping>

    <!--springMVC的核心控制转发器-->
    <servlet>
        <servlet-name>Spring MVC Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/spring-web.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <!--设置多个可接收的请求的格式-->
    <servlet-mapping>
        <servlet-name>Spring MVC Servlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Spring MVC Servlet</servlet-name>
        <url-pattern>*.fn</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Spring MVC Servlet</servlet-name>
        <url-pattern>*.page</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Spring MVC Servlet</servlet-name>
        <url-pattern>*.leaf</url-pattern>
    </servlet-mapping>

    <!--spring security组件,负责监听session的建立和销毁,发布HttpSessionEvent事件-->
    <listener>
        <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
    </listener>

    <!--spring security组件,负责指定需要调用的安全链springSecurityFilterChain-->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class> org.springframework.security.util.FilterToBeanProxy</filter-class>
        <init-param>
            <param-name>targetBeanName</param-name>
            <param-value>springSecurityFilterChain</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>

    <!--定制首页-->
    <welcome-file-list>
        <welcome-file>main.page</welcome-file>
    </welcome-file-list>

</web-app>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值