web.xml配置文件元素解释

<!-- 
每个xml文档开始都由xml序言开始
version="1.0"告诉解析器和浏览器,这个文件是按照1.0版本进行解析
encoding="UTF-8"表示此xml文档采用UTF-8的编码格式
 -->
<?xml version="1.0" encoding="UTF-8"?>
<!-- 
schema文档即xml schema document,schema文件的格式为.xsd
schema就是对xml的进一步约束,一旦确定了web.xml schema的引用,
就表示要参考该引用所在位置的schema定义和约束的规则

version="3.0"表示3.0版本

xmlns="http://java.sun.com/xml/ns/javaee" 
表示web.xml这个文件的命名空间为http://java.sun.com/xml/ns/j2ee

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
表示用xsi代替长的url,即http://www.w3.org/2001/XMLSchema-instance

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
这一句才指定了,web.xml真正遵循的约束,即xsd文件所在的位置
 -->
<web-app version="3.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 用编辑器打开该文档时,显示的就是这个名字 -->
  <display-name>shop</display-name>
<!-- 该标签声明的是系统运行时第一个显示的页面 -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
<!-- 以.action的请求都会被struts2过滤器过滤
    2.1.3之前的版本用org.apache.struts2.dispatcher.FilterDispatcher,之后的用下面的配置,从Struts2.1.3开始,
    将废弃ActionContextCleanUp过滤器,而在StrutsPrepareAndExecuteFilter过滤器中包含相应的功能,
    版本不同,错误的配置会报异常。 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>
    	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
<!-- 当前台jsp页面和后台Java代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据
  或者上传、下载中文名称文件出现乱码的问题,需要在web.xml中配置编码拦截器 -->
  <filter>    
    <filter-name>encodingFilter</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>encodingFilter</filter-name>    
    <url-pattern>/*</url-pattern>    
  </filter-mapping>
   <!-- 项目自己的过滤器,此处实现防止用户未登录就进入系统 -->
  <filter>
  <filter-name>userFilter</filter-name>
  	<filter-class>com.shop.filter.UserFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>userFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 配置错误页面,当状态码为404时,即返回指定页面 -->
  <error-page>
  	<error-code>404</error-code>
	<location>/error/index.jsp</location>
  </error-page>
  <!-- 在web.xml中配置spring,web.xml中contextConfigLocation指定你的配置文件所在的位置
  classpath:只会到你的class路径中查找找文件;   
  classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找. -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext*.xml</param-value>
  </context-param>
  <!-- 监听器启动自动优先于过滤器,因此即使将监听器放在过滤器之后也没有影响 -->
  <listener>
  		<!-- ContextLoaderListener是在启动Web容器时,自动装配ApplicationContext的配置信息。
  		它实现了ServletContextListener接口,配置该监听器,启动容器时,会默认执行它实现的方法。
		 -->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        <!-- 这个监听器是tomcat实例化的 -->
  </listener>
  <!-- 此监听器为项目自己的监听器,在项目启动时读取数据库记录 -->
  <listener>
  	<listener-class>com.shop.listener.InitDataListener</listener-class>
  </listener>
</web-app>
<!-- 
	此外,还有较为常用的log4j日志配置,它能给我们带来很多的便利之处,可以实现如下功能
	动态的改变记录级别和策略,即修改log4j.properties,不需要重启Web应用。
	把log文件定在 /WEB-INF/logs/ 而不需要写绝对路径。
	可以把log4j.properties和其他properties一起放在/WEB-INF/ ,而不是Class-Path。
	配置如下

	<context-param>    
        <param-name>log4jConfigLocation</param-name>    
        <param-value>classpath:log4j.properties</param-value>    
    </context-param>    
    <listener>    
        <listener-class>    
            org.springframework.web.util.Log4jConfigListener     
        </listener-class>    
    </listener>
 -->

猜你喜欢

转载自blog.csdn.net/heuguangxu/article/details/72861574