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>

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

SpringMVC---web.xml配置详解的更多相关文章

  1. java web.xml配置详解(转)

    源出处:java web.xml配置详解 1.常规配置:每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了我们站台的配置设定. web.xml定义: .站台的名称和说明 .针对环境参 ...

  2. web.xml配置详解之listener

    web.xml配置详解之listener 定义 <listener> <listener-class>nc.xyzq.listener.WebServicePublishLis ...

  3. Spring 入门 web.xml配置详解

    Spring 入门 web.xml配置详解 https://www.cnblogs.com/cczz_11/p/4363314.html https://blog.csdn.net/hellolove ...

  4. Web.xml配置详解(转)

    Web.xml配置详解 Posted on 2010-09-02 14:09 chinaifne 阅读(295105) 评论(16) 编辑 收藏 1 定义头和根元素 部署描述符文件就像所有XML文件一 ...

  5. Java web.xml 配置详解

    在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...

  6. java web.xml配置详解

    1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...

  7. 160329(二)、web.xml配置详解

    1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...

  8. Java Web学习总结(19)——web.xml配置详解

    1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...

  9. Maven系列--web.xml 配置详解

    一 .web.xml介绍 启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 紧接着,容 ...

  10. Spring 及 SpringMVC的web.xml配置详解

    出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在w ...

随机推荐

  1. 【起航计划 009】2015 起航计划 Android APIDemo的魔鬼步伐 08 App->Activity->QuickContactsDemo 联系人 ResourceCursorAdapter使用 QuickContactBadge使用

    QuickContactsDemo示例介绍了如何使用Content Provider来访问Android系统的Contacts 数据库. Content Provider为不同应用之间共享数据提供了统 ...

  2. php的yii框架开发总结2

    开发流程:1.用yii创建网站目录,当时用命令行创建时遇到了问题,试了很久才找到原因:我的原因是在yii/framework/yiic.bat这个文件中的一条语句: if "%PHP_COM ...

  3. mybatis由JDBC的演化过程分析

    我们知道,mybatis是对JDBC的封装,那么他是如何演变过来的呢? 摘自传智传媒Java培训资料 关于mybatis的演化原理,我们先看看我们最熟悉也是最基础的通过JDBC查询数据库数据,一般需要 ...

  4. 关于VisualStudio2010发布项目问题

    VisualStudio2010速度还是很给力的,VS2015打开机器就双100%了:VS2010机器上跑起来还是很好用的. 今天编译一个MVC3.0项目,发布时候出现诡异现象:Content文件夹里 ...

  5. 3dsmax2014的下载、安装与注册激活教程详解

    3dsmax2014的下载.安装与注册激活教程,虽然网上类似的教程文章不胜枚举,但大多比较粗枝大叶,没有详细的步骤,尤其对于电脑小白来说,更是不易参考,今天我就教大家如何注册破解3dsmax2014吧 ...

  6. [原创][Windows] Win7安装visual c++ 2015 redistributable x64失败

    在win7中安装visual c++ 2015 redistributable x64 时会卡住,原因是visual c++ 2015 redistributable x64 需要KB2999226, ...

  7. 504. Inverted Index (Map Reduce) lintcode

    https://www.lintcode.com/problem/inverted-index-map-reduce/description -- decription of the map redu ...

  8. Locust的官网及安装

    Locust官网: https://docs.locust.io/en/latest/installation.html for Python 3: $ python3 -m pip install ...

  9. mysql5.6之前需要账号的安全加固

    mysql5.6之前需要账号的安全加固 从5.7开始就不需要了. delete from mysql.user where user!='root' or host='localhost'; flus ...

  10. ceph-块存储客户端

    ceph块存储 ceph块设备,以前称为RADOS块设备,为客户机提供可靠性.分布式和高性能的块存储磁盘.RADOS块设备利用librbd库并以顺序的形式在ceph集群的多个osd上存储数据块.RBD ...