web.xml版本配置及常用属性配置

Web.xml版本配置以及常用属性配置

版本配置

web.xml v2.3

<?xml version="1.0" encoding="ISO-8859-1"?>  

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">  

<web-app></web-app>  

web.xml v2.4

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

<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"   

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  

</web-app>  

web.xml v2.5

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

<web-app 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_2_5.xsd"  version="2.5">  

</web-app> 

 web.xml v3.0

<?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"  version="3.0">  

</web-app>  

Web.xml加载过程

1. 启动WEB项目,容器首先会项目配置web.xml读取两个节点:  <listener></listener>和<context-param></context-param>。

2. 紧接着,容器创建一个ServletContext(application),这个WEB项目所有部分都将共享这个上下文。

3. 容器以<context-param></context-param>的name作为键,value作为值,将其转化为键值对,存入ServletContext。

4. 容器创建<listener></listener>中的类实例,根据配置的class类路径<listener-class>来创建监听,在监听中会有contextInitialized(ServletContextEvent args)初始化方法,启动Web应用时,系统调用Listener的该方法,在这个方法中获得:

<span style="fontfamily:Times New Roman;">

ServletContextapplication=ServletContextEvent.getServletContext();

</span> 

context-param的值就是application.getInitParameter("context-param的键");

6. 接着,容器会读取<filter></filter>,根据指定的类路径来实例化过滤器。

web.xml的加载顺序是:<context-param>-><listener>-><filter>-><servlet>。其中,如果web.xml中出现了相同的元素,则按照在配置文件中出现的先后顺序来加载。

Web.xml标签详解

<display-name>:定义web应用的名称

<distributable>:该元素告诉servlet/JSP容器,web容器中部署的应用适合在分布式环境下运行

<context-param>:使用上下文初始化参数;<param-name>指定参数名,<param-value>指定参数值,可选参数<description>描述参数

<session-config>:设置容器的session参数,子参数<session-timeout>用于指定http session的失效时间,即默认会话的超时时间,以分钟为单位,值为0或者负数时代表永不超时

<listener>:为web容器定义各种监听器,用来监听事件。子属性<listener-class>,如session事件和application事件,监听器功能取决于各自实现的接口,常用接口有:

ServletContextListener:用于监听Web应用的启动和关闭;

ServletContextAttributeListener:用于监听ServletContext范围(application)内属性的改变;

ServletRequestListener:用于监听用户的请求;

ServletRequestAttributeListener:用于监听ServletRequest范围(request)内属性的改变;

HttpSessionListener:用于监听用户session的开始和结束;

HttpSessionAttributeListener:用于监听HttpSession范围(session)内属性的改变。

为Web应用配置Listener的两种方式:

(1). 使用@WebListener修饰Listener实现类即可。

(2). 在web.xml文档中使用<listener>进行配置。

<filter>用于对用户请求request进行预处理,也可以对Response进行后处理,是个典型的处理链。子属性<filter-name><filter-class><url-pattern><init-param><param-name><param-value><icon><display-name><description>

使用Filter的流程:Filter对用户请求进行预处理,接着将请求HttpServletRequest交给Servlet进行处理并生成响应,最后Filter再对服务器响应HttpServletResponse进行后处理。

Filter使用场景:

  1. 认证Filter
  2. 日志和审核Filter
  3. 图片转换Filter
  4. 数据压缩Filter
  5. 密码Filter
  6. 令牌Filter
  7. 触发资源访问事件的Filter
  8. XSLT Filter
  9. 媒体类型链Filter

Filter可负责拦截多个请求或响应;一个请求或响应也可被多个Filter拦截。创建一个Filter只需两步:

创建Filter处理类
Web.xml文件中配置Filter

Filter必须实现javax.servlet.Filter接口,在该接口中定义了三个方法:

(1) void init(FilterConfig config):用于完成Filter的初始化。FilteConfig用于访问Filter的配置信息。
(2) void destroy():用于Filter销毁前,完成某些资源的回收。
(3) void doFilter(ServletRequest request,ServletResponse response,FilterChain chain):实现过滤功能的核心方法,该方法就是对每个请求及响应增加额外的处理。该方法实现对用户请求request进行预处理,也可以实现对服务器响应response进行后处理---它们的分界线为是否调用了chain.doFilter(request,response),执行该方法之前,即对用户请求request进行预处理,执行该方法之后,即对服务器响应response进行后处理。

配置Filter有两种方式:

(1). 在Filter类中通过Annotation进行配置。

(2). 在web.xml文件中通过配置文件进行配置。

Filter使用場景

字符集过滤器

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

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

CharacterEncodingFilter类可以通过简单配置来帮我们实现字符集转换的功能,参数encoding用于指定编码类型,参数forceEncoding设为true时,强制执行request.setCharacterEncoding(this.encoding)和reponse.setCharacterEncoding(this.encoding)中的方法。

缓存控制

<filter> 

    <filter-name>NoCache Filter</filter-name> 

    <filter-class>com.yonyou.mcloud.cas.client.authentication.NoCacheFilter</filter-class> 

</filter> 

  <filter-mapping> 

<filter-name>NoCache Filter</filter-name> 

<!—表示对URL全部过滤--> 

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

</filter-mapping>

登录认证

<!-- 认证过滤器 --> 

  <filter> 

    <filter-name>CAS Authentication Filter</filter-name> 

<filter-class>com.yonyou.mcloud.cas.client.authentication.ExpandAuthenticationFilter</filter-class> 

<init-param> 

      <param-name>casServerLoginUrl</param-name> 

      <param-value>https://dev.yonyou.com:443/sso-server/login</param-value> 

    </init-param> 

    <init-param> 

      <!--这里的server是服务端的IP --> 

      <param-name>serverName</param-name> 

      <param-value>http://10.1.215.40:80</param-value> 

    </init-param> 

  </filter> 

  <filter-mapping> 

     <filter-name>CAS Authentication Filter</filter-name> 

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

  </filter-mapping>

登录认证,未登录用户导向CAS Server进行认证。

单点登出

<!-- 认证过滤器 --> 

  <filter> 

    <filter-name>CAS Authentication Filter</filter-name> 

<filter-class>com.yonyou.mcloud.cas.client.authentication.ExpandAuthenticationFilter</filter-class> 

<init-param> 

      <param-name>casServerLoginUrl</param-name> 

      <param-value>https://dev.yonyou.com:443/sso-server/login</param-value> 

    </init-param> 

    <init-param> 

      <!--这里的server是服务端的IP --> 

      <param-name>serverName</param-name> 

      <param-value>http://10.1.215.40:80</param-value> 

    </init-param> 

  </filter> 

  <filter-mapping> 

     <filter-name>CAS Authentication Filter</filter-name> 

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

  </filter-mapping>

CAS Server通知CAS Client,删除session,注销登录信息。

封装request

<filter> 

    <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> 

    <filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class> 

</filter> 

<filter-mapping> 

    <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name> 

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

</filter-mapping>

封装request, 支持getUserPrincipal等方法。

存放Assertion到ThreadLocal中

<filter> 

    <filter-name>CAS Assertion Thread Local Filter</filter-name> 

    <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class> 

</filter> 

<filter-mapping> 

    <filter-name>CAS Assertion Thread Local Filter</filter-name> 

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

</filter-mapping>

禁用浏览器缓存

<filter> 

    <filter-name>NoCache Filter</filter-name> 

    <filter-class>com.yonyou.mcloud.cas.client.authentication.NoCacheFilter</filter-class> 

 </filter> 

 <filter-mapping> 

    <filter-name>NoCache Filter</filter-name> 

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

 </filter-mapping>

CAS Client向CAS Server进行ticket验证

<!-- 验证ST/PT过滤器 --> 

<filter> 

   <filter-name>CAS Validation Filter</filter-name> 

    <filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class> 

   <init-param> 

      <param-name>casServerUrlPrefix</param-name> 

      <param-value>https://dev.yonyou.com:443/sso-server</param-value> 

   </init-param>  

   <init-param> 

      <param-name>serverName</param-name> 

      <param-value>http://10.1.215.40:80</param-value> 

   </init-param> 

   <init-param> 

      <param-name>proxyCallbackUrl</param-name> 

      <param-value>https://dev.yonyou.com:443/business/proxyCallback</param-value> 

   </init-param> 

   <init-param> 

      <param-name>proxyReceptorUrl</param-name> 

      <param-value>/proxyCallback</param-value> 

   </init-param> 

   <init-param> 

      <param-name>proxyGrantingTicketStorageClass</param-name> 

<param-value>com.yonyou.mcloud.cas.client.proxy.MemcachedBackedProxyGrantingTicketStorageImpl</param-value> 

   </init-param> 

   <!-- 解决中文问题 --> 

   <init-param> 

      <param-name>encoding</param-name> 

      <param-value>UTF-8</param-value> 

   </init-param> 

</filter> 

<filter-mapping> 

    <filter-name>CAS Validation Filter</filter-name> 

    <url-pattern>/proxyCallback</url-pattern> 

</filter-mapping> 

<filter-mapping> 

    <filter-name>CAS Validation Filter</filter-name> 

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

</filter-mapping>

<servlet>用于处理及响应客户的请求。Servlet继承于HttpServlet,客户端通常只有GET和POST两种请求方式,Servlet为了响应则两种请求,必须重写doGet()和doPost()方法。大部分时候,Servlet对于所有的请求响应都是完全一样的,此时只需要重写service()方法即可响应客户端的所有请求。子属性有<servlet-name><servlet-class><irl-pattern><icon><display-name><description><jsp-file><load-on-startup>

HttpServlet有两个方法:

(1). init(ServletConfig config):创建Servlet实例时,调用该方法的初始化Servlet资源。

(2). destroy():销毁Servlet实例时,自动调用该方法的回收资源。

Servlet的生命周期:

创建Servlet实例有两个时机:

(1). 客户端第一次请求某个Servlet时,系统创建该Servlet的实例,大部分Servlet都是这种Servlet。

(2). Web应用启动时立即创建Servlet实例,即load-on-start Servlet。

每个Servlet的运行都遵循如下生命周期:

(1). 创建Servlet实例。

(2). Web容器调用Servlet的init()方法,对Servlet进行初始化。

(3). Servlet初始化后,将一直存在于容器中,用于响应客户端请求,如果客户端发送GET请求,容器调用Servlet的doGet()方法处理并响应请求;如果客户端发送POST请求,容器调用Servlet的doPost()方法处理并响应请求。或者统一使用service()方法处理来响应用户请求。

(4). Web容器决定销毁Servlet时,先调用Servlet的destory()方法,通常在关闭Web应用时销毁Servlet实例。

Servlet配置:

(1). 在Servlet类中使用@WebServlet Annotation进行配置。

(2). 在web.xml文件中进行配置。

示例代码

<!-- Spring view分发器  对所有的请求都由business对应的类来控制转发 --> 

<servlet> 

    <servlet-name>business</servlet-name> 

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

    <init-param> 

      <param-name>publishContext</param-name> 

      <param-value>false</param-value> 

    </init-param> 

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

</servlet>

示例代码二:

<servlet> 

    <servlet-name>spring</servlet-name> 

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

      <init-param> 

        <param-name>publishContext</param-name> 

        <param-value>false</param-value> 

</init-param> 

 <init-param>   

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

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

        </init-param>  

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

</servlet>

contextLoadListener与DispatcherServlet的区别

ContextLoaderListener初始化的上下文加载的Bean是对于整个应用程序共享的,一般如:DAO层、Service层Bean;DispatcherServlet初始化的上下文加载的Bean是只对Spring MVC有效的Bean,如:Controller、HandlerMapping、HandlerAdapter等,该初始化上下文只加载Web相关组件。

<swelcome-file-list>指定首页文件名称。子元素<welcome-file>

Spring框架下web.xml配置

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

 

    <!-- 在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?

    下面我们来看看Spring框架给我们提供过滤器CharacterEncodingFilter 

     这个过滤器就是针对于每次浏览器请求进行过滤的,然后再其之上添加了父类没有的功能即处理字符编码。 

      其中encoding用来设置编码格式,forceEncoding用来设置是否理会 request.getCharacterEncoding()方法,设置为true则强制覆盖之前的编码格式。--> 

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

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

    <!-- 项目中使用Spring 时,applicationContext.xml配置文件中并没有BeanFactory,要想在业务层中的class 文件中直接引用Spring容器管理的bean可通过以下方式--> 

    <!--1、在web.xml配置监听器ContextLoaderListener--> 

    <!--ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。 

    在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。 

    它的API说明 

    第一段说明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。 

    如果查看ContextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它实现了HttpServlet这个接口 

    第二段,ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext-> 

    BeanFactory这样一来spring中的所有bean都由这个类来创建 

     IUploaddatafileManager uploadmanager = (IUploaddatafileManager)    ContextLoaderListener.getCurrentWebApplicationContext().getBean("uploadManager");

     --> 

    <listener> 

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

    </listener> 

    <!--2、部署applicationContext的xml文件--> 

    <!--如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml, 

    在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。 

    如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数: 

    在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并以“,”号分隔。 

    也可以这样applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml, 

    applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。 

    在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。--> 

    <context-param> 

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

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

    </context-param> 

 

    <!--如果你的DispatcherServlet拦截"/",为了实现REST风格,拦截了所有的请求,那么同时对*.js,*.jpg等静态文件的访问也就被拦截了。--> 

    <!--方案一:激活Tomcat的defaultServlet来处理静态文件--> 

    <!--要写在DispatcherServlet的前面, 让 defaultServlet先拦截请求,这样请求就不会进入Spring了,我想性能是最好的吧。--> 

    <servlet-mapping> 

        <servlet-name>default</servlet-name> 

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

    </servlet-mapping> 

    <servlet-mapping> 

        <servlet-name>default</servlet-name> 

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

    </servlet-mapping> 

    <servlet-mapping> 

        <servlet-name>default</servlet-name> 

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

    </servlet-mapping> 

    <servlet-mapping> 

        <servlet-name>default</servlet-name> 

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

    </servlet-mapping> 

    <servlet-mapping> 

        <servlet-name>default</servlet-name> 

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

    </servlet-mapping> 

    <servlet-mapping> 

        <servlet-name>default</servlet-name> 

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

    </servlet-mapping> 

    <servlet-mapping> 

        <servlet-name>default</servlet-name> 

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

    </servlet-mapping> 

    <servlet-mapping> 

        <servlet-name>default</servlet-name> 

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

    </servlet-mapping> 

    <servlet-mapping> 

        <servlet-name>default</servlet-name> 

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

    </servlet-mapping> 

    <servlet-mapping> 

        <servlet-name>default</servlet-name> 

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

    </servlet-mapping> 

    <!--使用Spring MVC,配置DispatcherServlet是第一步。DispatcherServlet是一个Servlet,,所以可以配置多个DispatcherServlet--> 

    <!--DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理。--> 

    <servlet> 

        <servlet-name>DispatcherServlet</servlet-name><!--在DispatcherServlet的初始化过程中,框架会在web应用的 WEB-INF文件夹下寻找名为[servlet-name]-servlet.xml 的配置文件,生成文件中定义的bean。--> 

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

        <!--指明了配置文件的文件名,不使用默认配置文件名,而使用dispatcher-servlet.xml配置文件。--> 

        <init-param> 

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

            <!--其中<param-value>**.xml</param-value> 这里可以使用多种写法--> 

            <!--1、不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml--> 

            <!--2、<param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value>--> 

            <!--3、<param-value>classpath*:dispatcher-servlet.xml</param-value>--> 

            <!--4、多个值用逗号分隔--> 

            <param-value>classpath:spring/dispatcher-servlet.xml</param-value> 

        </init-param> 

        <load-on-startup>1</load-on-startup><!--是启动顺序,让这个Servlet随Servletp容器一起启动。--> 

    </servlet> 

    <servlet-mapping> 

        <!--这个Servlet的名字是dispatcher,可以有多个DispatcherServlet,是通过名字来区分的。每一个DispatcherServlet有自己的WebApplicationContext上下文对象。同时保存的ServletContext中和Request对象中.--> 

        <!--ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”了:P,Spring把Bean放在这个容器中,在需要的时候,用getBean方法取出--> 

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

        <!--Servlet拦截匹配规则可以自已定义,当映射为@RequestMapping("/user/add")时,为例,拦截哪种URL合适?--> 

        <!--1、拦截*.do、*.htm, 例如:/user/add.do,这是最传统的方式,最简单也最实用。不会导致静态文件(jpg,js,css)被拦截。--> 

        <!--2、拦截/,例如:/user/add,可以实现现在很流行的REST风格。很多互联网类型的应用很喜欢这种风格的URL。弊端:会导致静态文件(jpg,js,css)被拦截后不能正常显示。 --> 

        <url-pattern>/</url-pattern> <!--会拦截URL中带“/”的请求。--> 

    </servlet-mapping> 

 

    <welcome-file-list><!--指定欢迎页面--> 

        <welcome-file>login.html</welcome-file> 

    </welcome-file-list> 

    <error-page> <!--当系统出现404错误,跳转到页面nopage.html--> 

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

        <location>/nopage.html</location> 

    </error-page> 

    <error-page> <!--当系统出现java.lang.NullPointerException,跳转到页面error.html--> 

        <exception-type>java.lang.NullPointerException</exception-type> 

        <location>/error.html</location> 

    </error-page> 

    <session-config><!--会话超时配置,单位分钟--> 

        <session-timeout>360</session-timeout> 

    </session-config> 

</web-app>

步骤:

1、spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 
2、在web.xml配置监听器ContextLoaderListener(listener-class) 
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。 
3、部署applicationContext的xml文件:contextConfigLocation(context-param下的param-name) 
4、DispatcherServlet是前置控制器,配置在web.xml文件中的。拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller(我们写的Action)来处理。 
DispatcherServlet(servlet-name、servlet-class、init-param、param-name(contextConfigLocation)、param-value) 
在DispatcherServlet的初始化过程中,框架会在web应用的 WEB-INF文件夹下寻找名为[servlet-name]-servlet.xml 的配置文件,生成文件中定义的bean

Spring配置文件:

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

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"

    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"

    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

    <!-- 配置databaseSource -->

    <!-- ====================================== -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

        <property name="url"

            value="jdbc:mysql://localhost:3306/storemanager?characterEncoding=utf-8"></property>

        <property name="username" value="root"></property>

        <property name="password" value="123456"></property>

        <!-- 连接池初始化连接个数 -->

        <property name="initialSize" value="3" />

        <!-- 连接池的最大值 -->

        <property name="maxActive" value="10" />

        <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->

        <property name="maxIdle" value="5" />

        <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->

        <property name="minIdle" value="2" />

        <!-- 获取连接最大等待时间 -->

        <!-- <property name="maxWait" value="60000" /> -->

 

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->

        <property name="timeBetweenEvictionRunsMillis" value="60000" />

        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->

        <property name="minEvictableIdleTimeMillis" value="25200000" />

        <!-- 打开removeAbandoned功能 -->

        <property name="removeAbandoned" value="true" />

        <!-- 1800秒,也就是30分钟 -->

        <property name="removeAbandonedTimeout" value="1800" />

        <!-- 关闭abanded连接时输出错误日志 -->

        <property name="logAbandoned" value="true" />

        <!-- 监控数据库 -->

        <!-- <property name="filters" value="mergeStat" /> -->

        <!-- <property name="filters" value="stat" /> -->

    </bean>

 

    <!-- 配置sessionFactory,用于获取session -->

    <!-- ====================================== -->

    <bean id="sessionFactory"

        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource">

            <ref bean="dataSource" />

        </property>

        <property name="mappingResources">

            <list>

                <value>com/kl/napchen/store/bean/User.hbm.xml</value>

                <value>com/kl/napchen/store/bean/ProductIn.hbm.xml</value>

                <value>com/kl/napchen/store/bean/ProductOut.hbm.xml</value>

                <value>com/kl/napchen/store/bean/ProductType.hbm.xml</value>

            </list>

        </property>

        <property name="hibernateProperties">

            <props>

                <prop key="hibernate.dialect">

                    org.hibernate.dialect.MySQL5Dialect

                </prop>

                <prop key="hibernate.hbm2ddl.auto">update</prop>

                <prop key="hibernate.format_sql">true</prop>

                <prop key="hibernate.show_sql">true</prop>

                <prop key="current_session_context_class">thread</prop>

            </props>

        </property>

 

        <!-- 自动扫描注解方式配置的hibernate类文件 -->

        <!-- <property name="packagesToScan"> -->

        <!-- <list> -->

        <!-- <value>light.mvc.model</value> -->

        <!-- </list> -->

        <!-- </property> -->

    </bean>

 

    <!-- 配置事务管理器 -->

    <!-- ====================================== -->

    <bean id="transactionManager"

        class="org.springframework.orm.hibernate4.HibernateTransactionManager">

        <property name="sessionFactory" ref="sessionFactory" />

        <!-- <property name="rollbackOnCommitFailure" value="true" /> -->

    </bean>

 

    <!-- 注解方式配置事物 -->

    <!-- ====================================== -->

    <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->

 

    <!-- AOP方式配置事物 -->

    <!-- ====================================== -->

    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">

        <tx:attributes>

            <!-- <tx:method name="get*" propagation="REQUIRED" read-only="true" /> -->

            <!-- <tx:method name="add*" propagation="REQUIRED" /> -->

            <tx:method name="delete" propagation="REQUIRED" />

            <tx:method name="*" propagation="REQUIRED" />

        </tx:attributes>

    </tx:advice>

 

    <aop:config>

        <aop:pointcut id="transactionPointcut"

            expression="execution(* com.kl.napchen.store.impl..*.*(..))" />

        <aop:advisor pointcut-ref="transactionPointcut"

            advice-ref="transactionAdvice" />

    </aop:config>

 

    <!-- 启用aspectj注解自动代理 -->

    <!-- ====================================== -->

    <aop:aspectj-autoproxy />

 

    <!-- 配置代理bean -->

    <!-- ====================================== -->

    <bean id="adviceLog" class="com.kl.napchen.store.annotation.AdviceLog"></bean>

    <bean id="adviceUserLog" class="com.kl.napchen.store.annotation.AdviceUserLog"></bean>

 

    <!-- 配置Bean -->

    <!-- ====================================== -->

    <bean id="user" class="com.kl.napchen.store.bean.User"></bean>

    <bean id="productIn" class="com.kl.napchen.store.bean.ProductIn"></bean>

    <bean id="productOut" class="com.kl.napchen.store.bean.ProductOut"></bean>

    <bean id="productType" class="com.kl.napchen.store.bean.ProductType"></bean>

    <bean id="sessionInfo" class="com.kl.napchen.store.global.SessionInfo"></bean>

    <bean id="pageGrid" class="com.kl.napchen.store.page.PageGrid"></bean>

 

    <!-- 配置操作数据的Dao -->

    <!-- ====================================== -->

    <bean id="userDao" class="com.kl.napchen.store.dao.UserDao"></bean>

    <bean id="productInDao" class="com.kl.napchen.store.dao.ProductInDao"></bean>

    <bean id="productOutDao" class="com.kl.napchen.store.dao.ProductOutDao"></bean>

    <bean id="productTypeDao" class="com.kl.napchen.store.dao.ProductTypeDao"></bean>

 

    <!-- 配置业务处理的service -->

    <!-- ====================================== -->

    <bean id="userImpl" class="com.kl.napchen.store.impl.UserImpl"></bean>

    <bean id="productInImpl" class="com.kl.napchen.store.impl.ProductInImpl"></bean>

    <bean id="productOutImpl" class="com.kl.napchen.store.impl.ProductOutImpl"></bean>

    <bean id="productTypeImpl" class="com.kl.napchen.store.impl.ProductTypeImpl"></bean>

 

    <!-- 配置控制流程的controller -->

    <!-- ====================================== -->

    <!-- <bean id="userController" class="com.kl.napchen.store.controller.UserController"></bean> -->

    <!-- <bean id="productInController" class="com.kl.napchen.store.controller.ProductInController"></bean> -->

    <!-- <bean id="productOutController" class="com.kl.napchen.store.controller.ProductOutController"></bean> -->

    <!-- <bean id="productTypeController" class="com.kl.napchen.store.controller.ProductTypeController"></bean> -->

 

 

    <!-- 配置获取spring容器中Bean的工具Bean -->

    <!-- 由于持有ApplicationContext, -->

    <!-- 可以使用SpringContextHolder.getBean('xx')的静态方法得到spring bean对象 -->

    <!-- ====================================== -->

    <!-- <bean class="com.kl.napchen.storeManagerSystem.contextHolder.SpringContextHolder"

        lazy-init="false" /> -->

 

    <!-- 对静态资源文件的访问 方案一 (二选一) -->

    <!-- 使用"*.do"配置DispatcherServlet时不存在静态资源访问问题 ,拦截器将不会拦截静态资源的URL -->

    <!-- 使用"/"配置DispatcherServlet时存在静态资源访问问题,采用以下两种方案解决 -->

    <!-- ====================================== -->

    <mvc:default-servlet-handler />

 

    <!-- 对静态资源文件的访问 方案二 (二选一) -->

    <!-- ====================================== -->

    <!-- 静态资源映射 -->

    <!-- <mvc:resources mapping="/js/**" location="/WEB-INF/js/" /> -->

    <!-- <mvc:resources mapping="/css/**" location="/WEB-INF/css/" /> -->

    <!-- <mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" /> -->

    <!-- <mvc:resources mapping="/plugins/**" location="/WEB-INF/plugins/" /> -->

    <!-- <mvc:resources mapping="images/**" location="/WEB-INF/images/" /> -->

 

    <!-- 默认的注解映射的支持 -->

    <!-- 采用这下面种方式将自动装配DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->

    <!-- 采用这下面种方式将无法在 DefaultAnnotationHandlerMapping配置拦截器 -->

    <!-- ====================================== -->

    <mvc:annotation-driven />

 

    <!-- 配置 DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->

    <!-- ====================================== -->

    <!-- <bean -->

    <!-- class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> -->

    <!-- 配置拦截器 -->

    <!-- <property name="interceptors"> -->

    <!-- <list> -->

    <!-- <bean class="com/kl/napchen/storeManagerSystem/interceptor/MyInterceptor"></bean> -->

    <!-- </list> -->

    <!-- </property> -->

    <!-- </bean> -->

    <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

        </bean> -->

 

    <!-- 隐式地向 Spring容器注册这4个BeanPostProcessor -->

    <!-- AutowiredAnnotationBeanPostProcessor、 -->

    <!-- RequiredAnnotationBeanPostProcessor、 -->

    <!-- CommonAnnotationBeanPostProcessor、 -->

    <!-- PersistenceAnnotationBeanPostProcessor -->

    <!-- ====================================== -->

    <!-- <context:annotation-config /> -->

 

 

    <!-- 设置使用注解的类所在的jar包 ,使用这种即可省去上面的声明 -->

    <!-- ====================================== -->

    <context:component-scan base-package="com.kl.napchen.store.controller" />

    <context:component-scan base-package="com.kl.napchen.store.impl" />

    <context:component-scan base-package="com.kl.napchen.store.baseService" />

 

 

    <!-- 映射“/”的url -->

    <!-- ====================================== -->

    <!-- <mvc:view-controller path="/" view-name="forward:/index2" /> -->

 

    <!-- configure the InternalResourceViewResolver -->

    <bean

        class="org.springframework.web.servlet.view.InternalResourceViewResolver"

        id="internalResourceViewResolver">

        <!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"

            /> -->

        <!-- 前缀 -->

        <property name="prefix" value="/WEB-INF/jsp/" />

        <!-- 后缀 -->

        <property name="suffix" value=".jsp" />

    </bean>

 

    <!-- 自定义拦截器 (近似-总拦截器) -->

    <!-- ====================================== -->

    <mvc:interceptors>

        <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->

        <bean class="com.kl.napchen.store.interceptor.MyInterceptor" />

    </mvc:interceptors>

    <!-- 总错误处理 -->

    <!-- ====================================== -->

    <bean id="exceptionResolver"

    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">

        <!-- 配置不同类别的错误对应的view和状态码 -->

        <property name="exceptionMappings">

            <props>

                <prop key="java.lang.Exception">errors/error</prop>

                <prop key="java.lang.Throwable">errors/error</prop>

                <!-- 上传文件大于最大尺寸后转向出错页面 -->

                <!-- ====================================== -->

                <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">errors/uploadError

                </prop>

            </props>

        </property>

        <property name="statusCodes">

            <props>

                <prop key="errors/error">500</prop>

                <prop key="errors/404">404</prop>

            </props>

        </property>

        <!-- 设置日志输出级别,不定义则默认不输出警告等错误日志信息 -->

        <property name="warnLogCategory">

            <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver

            </value>

        </property>

        <!-- 默认错误页面,当找不到上面mappings中指定的异常对应视图时,使用本默认配置 -->

        <property name="defaultErrorView" value="errors/error"></property>

        <!-- 默认HTTP状态码 -->

        <property name="defaultStatusCode" value="500"></property>

    </bean>

</beans>

Springmvc配置:

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:tx="http://www.springframework.org/schema/tx" 
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-3.2.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 
<!-- 自动扫描的包名 --> 
<context:component-scan base-package="com.cn.*" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan>
<!-- 默认的注解映射的支持,自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter --> 
<mvc:annotation-driven /> 
<!-- 视图解释类 --> 
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<property name="prefix" value="/view/"/> 
<property name="suffix" value=".jsp"/> 
</bean> 
<!-- 对静态资源文件的访问--> 
<mvc:resources mapping="/images/**" location="/WEB-INF/images/" cache-period="31556926"/> 
<mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="31556926"/> 
<mvc:resources mapping="/css/**" location="/WEB-INF/css/" cache-period="31556926"/> 
</beans>

猜你喜欢

转载自blog.csdn.net/davidbieber/article/details/82896290