web应用的配置描述符--web.xml文件

Posted 吴二喵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web应用的配置描述符--web.xml文件相关的知识,希望对你有一定的参考价值。

配置描述符web.xml对于java web应用来说非常重要。

在servlet2.5规范之前,每个Java WEB应用都必须包含个web.xml文件,且必须在WEB-INF路径下。从servlet3.0开始,WEB-INF路径下的web.xml文件不再是必须的,但通常还是建议保留该配置文件。

Servlet2.5之前,Java Web应用的绝大部分组件都通过web.xml文件来配置管理,从servlet3.0开始,也可以通过注解来配置管理wb组件,因此web.xml文件可以变得更简洁。

每个Web容器都会提供一个系统的web.xml文件,用于描述所有web应用共同的配置属性。例如Tomcat的系统web.xml放在Tomcat的conf路径下,而Jetty的系统web.xml文件放在Jetty的etc路径下,文件名为webdefault.xml。

web.xml配置文件的内容主要包括:

  1. 配置JSP
  2. 配置和管理Servlet
  3. 配置和管理Listener
  4. 配置和管理Filter
  5. 配置标签库
  6. 配置JSP属性
    除此之外,web.xml还负责配置,管理如下常用内容:

  7. 配置和管理JAAS授权认证

  8. 配置和管理资源医用
  9. web应用首页

web.xml文件的根元素是<web-app.../>元素,在Servlet3.0规范中,该元素新增了如下属性:
metadata-complete:该属性接受true和false两个属性值。当该属性值为true时,该web应用将不会加载注解配置的web组件(如servlet、Filter、Listener等)

在web.xml文件中配置首页使用welcome-file-list元素,该元素能包含多个welcom-file子元素,其中每个welcom-file子元素配置一个首页。

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

上面的配置信息指定该web应用的首页依次是index.html、index.jsp。

web.xml中配置前端控制器示例如下:

<servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

contextConfigLocation配置Spring MVC加载的配置文件(配置处理器映射器、处理器适配器等)
如果不配置,默认加载的是/WEB-INF/servlet名称-servlet.xml,如springmvc-servlet.xml。

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

指定配置文件的配置。

<url-pattern>有两种配置方式。

第一种*.action 访问以.action结尾由DispatcherServlet解析
第二种 / 表示所有访问的地址都由DispatcherServlet解析,对于静态文件的解析需要配置不让 DispatcherServlet解析,使用此方式可以实现RESTful风格的url

如果web应用需要额外加载配置文件,可以如下的配置方式导入所需的配置文件

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml,classpath:spring-property.xml</param-value>
</context-param>

filter的配置如下:

  <filter>
        <filter-name>UserValidateFilter</filter-name>
        <display-name>UserValidateFilter</display-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetBeanName</param-name>
            <param-value>userValidateFilterBean</param-value>
        </init-param>
    </filter>
    <filter>
        <filter-name>WebAccessAuthorizeFilter</filter-name>
        <display-name>UserValidateFilter</display-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetBeanName</param-name>
            <param-value>webAccessAuthorizeFilterBean</param-value>
        </init-param>
    </filter>


    <!-- 按如下顺序在web.xml内添加Filter Mapping配置  -->

    <filter-mapping>
        <filter-name>UserValidateFilter</filter-name>
        <url-pattern>/pages/*</url-pattern>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>WebAccessAuthorizeFilter</filter-name>
        <url-pattern>/pages/*</url-pattern>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>

    <filter>
        <filter-name>encodingFilter</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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

listener的配置如下:

<!-- Spring监听器 -->
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

    <!-- 防止Spring内存溢出监听器 -->
<listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

下面说说spring-mvc.xml的配置

</mvc:annotation-driven> 

可以代替注解映射器和适配器配置,它默认加载很多的参数绑定方法,比如json转换解析默认加载了。

//在dispatch-content.xml配置打开允许矩阵请求格式
<mvc:annotation-driven enable-matrix-variables="true">
</mvc:annotation-driven>  

对于注解的handler可以单个配置,但一个个配置比较麻烦,则可以使用组件扫描,扫描controller,service,component组件…,指定扫描的包

<context:component-scan base-package="cn.crystal.springdemo.controller" />

文件配置示例

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                            http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
                            http://www.springframework.org/schema/context  
                            http://www.springframework.org/schema/context/spring-context-4.2.xsd  
                            http://www.springframework.org/schema/mvc  
                            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
                            http://www.springframework.org/schema/cache 
                            http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">


    <!-- 自动扫描该包-->
    <context:component-scan base-package="cn.crystal.crsweb.controller" />

    <!-- Handler Mapping -->
    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    </bean>

    <!-- HandlerAdapter -->
    <bean id="handlerAdapter"
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="customJsonHttpMessageConverter" /> <!-- JSON转换器 -->
                <bean
                    class="org.springframework.http.converter.StringHttpMessageConverter"> <!-- 原默认字符串转换器 -->
                    <property name="writeAcceptCharset" value="false"></property> <!-- // see SPR-7316 -->
                </bean>
            </list>
        </property>
    </bean>


    <!-- 对静态资源的访问 -->
    <mvc:resources mapping="/js/**" location="/js/" /> 
    <mvc:resources mapping="/css/**" location="/css/" />

    <!-- 添加注解驱动 -->
    <mvc:annotation-driven enable-matrix-variables="true" />
    <!-- 允许对静态资源文件的访问 -->

    <mvc:default-servlet-handler />

    <!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
        <property name="prefix" value="/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 自定义的JSON转换器 -->
    <bean id="customJsonHttpMessageConverter"
        class="com.crystal.crsweb.controller.converter.GbMappingJackson2HttpMessageConverter">
    </bean>
</beans>

以上是关于web应用的配置描述符--web.xml文件的主要内容,如果未能解决你的问题,请参考以下文章

关于Java Web应用中的配置部署描述符web.xml

部署描述符:web.xml

web.xml 文件配置01

web.xml 部署描述符元素

Deloyment Descriptor web.xml

Descriptor&web.xml