• SpringMVC---web.xml配置详解


    web.xml中需要配置的内容

    1.配置监听器<listener>

             它有两个监听器:

              1).

               <!--配置文件加载监听器-->

              <listener>

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

             </listener>

              ContextLoaderListener它的作用是启动web容器,(加载配置文件)自动装配applicationContext.xml配置信息(详细配置见下面)。

              2).

              <!--spring  log4j日志监听器配置-->

             <listener>

                   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

             </listener>

             <context-param>

                 <param-name>Log4jConfigListener</param-name>

                 <!-- spring刷新log4j文件的时间间隔 -->

                 <param-value>10000</param-value>

             </context-param>

    2.部署applicationContext.xml文件

            如果不写任何参数配置,默认的是在/WEB-INF/applicationContext.xml

            如果想要自定义文件名,需要在web.xml中加入contextConfigLocation这个context参数

             <!-- 配置applicationContext.xml -->

             <context-param>

              <param-name>contextConfigLocation</param-name>

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

             </context-param>

    3.配置前端控制器DispatcherServlet:它是拦截请

            <!-- 配置DispatchServlet 的核心控制器 -->

            DispatchServlet是HTTP请求的中央调度处理器,它将web请求转发给controller层处理,它提供了敏捷的映射和异常处理机制。

    <servlet>
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <!-- 指定SpringMVC配置文件 -->
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:config/springmvc.xml</param-value>
      </init-param>
    </servlet>
    <servlet-mapping>
      <!-- 设置http请求拦截,如*.do,这里设置的是拦截所有 -->
      <servlet-name>DispatcherServlet</servlet-name>   <url-pattern>/</url-pattern> </servlet-mapping>

        4.<!-- 错误跳转页面 -->

         <error-page>

          <!-- 路径不正确 -->

          <error-code>404</error-code>

          <location>/WEB-INF/errorpage/404.jsp</location>

         </error-page>

         <error-page>

          <!-- 没有访问权限,访问被禁止 -->

          <error-code>405</error-code>

          <location>/WEB-INF/errorpage/405.jsp</location>

         </error-page>

         <error-page>

          <!-- 内部错误 -->

          <error-code>500</error-code>

          <location>/WEB-INF/errorpage/500.jsp</location>

         </error-page>

        5.配置字符集编码

        <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>

           <!-- forceEncoding用来设置是否理会 request.getCharacterEncoding的方法 -->

              <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>

        以上就是我们经常会用到的东西。

  • 相关阅读:
    Atitit.code base view 视图的实现原理
    Atitit.code base view 视图的实现原理
    Atitit。  工作流引擎的发展趋势
    Atitit. atiOrder   Order 订单管理框架的设计
    Atitit。  工作流引擎的发展趋势
    Atitit. atiOrder   Order 订单管理框架的设计
    atitit.编程语言 类与对象的 扩展机制.doc
    atitit.编程语言 类与对象的 扩展机制.doc
    Atitit.为什么小公司也要做高大上开源项目
    Atitit.为什么小公司也要做高大上开源项目
  • 原文地址:https://www.cnblogs.com/xiaobaizhiqian/p/8214255.html
Copyright © 2020-2023  润新知