您的位置:首页 > 编程语言 > Java开发

Springmvc中web.xml的配置详解

2017-03-16 11:14 393 查看
首先,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>

      <!-- 配置文件的位置 -->

      <param-value>classpath:log4j.properties</param-value>

 </context-param>

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

 </servlet>

 <servlet-mapping>

  <servlet-name>DispatcherServlet</servlet-name>

  <url-pattern>*.do</url-pattern>

 </servlet-mapping>

或者:

applicationContext.xml配置文件还可以使用<init-param>标签在servlet标签中进行配置

<servlet>

  <servlet-name>DispatcherServlet</servlet-name>

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

<!-- 初始化applicationContext.xml -->

     <init-param>

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

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

     </init-param>

 </servlet>

 <servlet-mapping>

  <servlet-name>DispatcherServlet</servlet-name>

  <url-pattern>*.do</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>

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

《*****************************下面贴上一个完整的示例:********************************》

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" 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_3_0.xsd">

 <display-name>Web Application</display-name>

 <!-- 读取spring配置文件 -->

 <context-param>

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

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

 </context-param>

 <!-- Spring字符集过滤器 -->

 <filter>

  <filter-name>SpringEncodingFilter</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-mapping>

  <filter-name>SpringEncodingFilter</filter-name>

  <url-pattern>/*</url-pattern>

 </filter-mapping>

 <filter>

  <filter-name>springSessionRepositoryFilter</filter-name>

  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

 </filter>

 <filter-mapping>

  <filter-name>springSessionRepositoryFilter</filter-name>

  <url-pattern>/*</url-pattern>

 </filter-mapping>

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

 <context-param>

  <param-name>log4jConfigLocation</param-name>

  <param-value>classpath:log4j.properties</param-value>

 </context-param>

 <context-param>

  <param-name>log4jRefreshInterval</param-name>

  <param-value>60000</param-value>

 </context-param>

 <listener>

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

 </listener>

 <listener>

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

 </listener>

 <listener>

  <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>

 </listener>

 <!-- springMVC核心配置 -->

 <servlet>

  <servlet-name>dispatcherServlet</servlet-name>

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

  <init-param>

  <!-- 注解式控制器 -->

   <param-name>dispatchOptionsRequest</param-name>

   <param-value>true</param-value>

  </init-param>

  <init-param>

   <param-name>dispatchTraceRequest</param-name>

   <param-value>true</param-value>

  </init-param>

  <init-param>

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

   <param-value>classpath:config/spring/applicationContext-mvc.xml</param-value>

  </init-param>

  <load-on-startup>1</load-on-startup>

 </servlet>

 <servlet-mapping>

  <servlet-name>dispatcherServlet</servlet-name>

  <url-pattern>*.do</url-pattern>

 </servlet-mapping>

 <welcome-file-list>

  <welcome-file>index.jsp</welcome-file>

 </welcome-file-list>

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

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

</web-app>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: