web.xml

来源:互联网 发布:欧洲中世纪知乎 编辑:程序博客网 时间:2024/06/18 04:41

    • ContextLoaderListener
    • contextConfigLocation
    • classpath和classpath
    • DispatcherServlet
  • CharacterEncodingFilter

本文会对web.xml中的各种参数进行一一详解,如有不对的地方,请大家指出来。

1.ContextLoaderListener

 <servlet>  <servlet-name>portal</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

作用:

  • 在启动Web容器时,自动装配Spring applicationContext.xml的配置信息。

2.contextConfigLocation

<context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext*.xml</param-value></context-param>

功能:

  • contextConfigLocation是指定的applicationContext.xml文件的加载路径,可以不配置,默认是/WEB-INF/applicationContext。配置后则会去加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。

3.classpath:和classpath*:

<context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext*.xml</param-value></context-param>

这一段配置应该是大家web.xml中都有的配置,ServletContext初始化参数

  • classpath:只会到你的class路径中查找找文件;
  • classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找.
  • -
<param-value>classpath*:applicationContext.xml,          classpath*:app-datasource.xml,          classpath*:app-memcached.xml  </param-value>  

4.DispatcherServlet

<servlet>  <servlet-name>portal</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>   <param-name>contextConfigLocation</param-name>   <param-value>classpath:webmvc.xml</param-value>  </init-param>  <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>  <servlet-name>portal</servlet-name>  <url-pattern>/</url-pattern> </servlet-mapping>
  • DispatcherServlet:
    1、文件上传解析,如果请求类型是multipart将通过MultipartResolver进行文件上传解析;
    2、通过HandlerMapping,将请求映射到处理器(返回一个HandlerExecutionChain,它包括一个处理器、多个HandlerInterceptor拦截器);
    3、通过HandlerAdapter支持多种类型的处理器(HandlerExecutionChain中的处理器);
    4、通过ViewResolver解析逻辑视图名到具体视图实现;
    5、本地化解析;
    6、渲染具体的视图等;
    7、如果执行过程中遇到异常将交给HandlerExceptionResolver来解析。

  • load-on-startup:表示启动容器时初始化该Servlet;

  • url-pattern:表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。

5.CharacterEncodingFilter

解决编码不统一的问题

<filter>        <filter-name>CharacterEncodingFilter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <async-supported>true</async-supported><!--异步支持 -->        <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-mapping>        <filter-name>CharacterEncodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>
原创粉丝点击