spring与web.xml相关配置

<!-- 指定spring字符过滤器 -->

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

<!-- 指定spring配置文件位置 -->

   <context-param>
          <param-name>contextConfigLocation</param-name>
   <param-value>
    <!--加载多个spring配置文件 -->
    /WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml,, /WEB-INF/action-servlet*.xml,classpath:spring/applicationContext*.xml
   </param-value>
</context-param>

<!-- 定义SPRING监听器,加载spring -->

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

<!---此处还不知道是做什么 用的->

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

  <!--设置 hibernate 的  session 在开始请求时候打开 在视图渲染后关闭  default is single-->

 <filter>
  <filter-name>OpenSessionInViewFilter</filter-name>
  <filter-class>
        org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  </filter-class>
  <init-param>
       <param-name>singleSession</param-name>
       <param-value>true</param-value>
  </init-param>
 </filter>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 <!-- spring  有三种启用模式 1ContextLoaderServlet 2.ContextLoaderListener 3.ContextLoaderPlugIn-->
 <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/applicationContext*.xml</param-value>
 </context-param>

<!-- strut配置文件设置 -->

<servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>
       org.apache.struts.action.ActionServlet
  </servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>
       /WEB-INF/struts-config.xml,/WEB-INF/struts-config-framework.xml
   </param-value>
  </init-param>
  <load-on-startup>0</load-on-startup>
 </servlet>

 <!-- Action -->
 <servlet-mapping>
     <servlet-name>action</servlet-name>
     <url-pattern>*.do</url-pattern>
 </servlet-mapping>

  

 <!-- Spring过滤中文字符集 -->

 <filter>
  <filter-name>SetCharacterEncoding</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>SetCharacterEncoding</filter-name>
     <url-pattern>*.jsp</url-pattern>
 </filter-mapping>
 <filter-mapping>
     <filter-name>SetCharacterEncoding</filter-name>
     <url-pattern>*.do</url-pattern>
 </filter-mapping>

 <!-- 注册Spring的request作用域 -->

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

 <!--

  request

  request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效,配置实例:

  request、session、global session使用的时候首先要在初始化web的web.xml中做如下配置:

  如果你使用的是Servlet 2.4及以上的web容器,那么你仅需要在web应用的XML声明文件web.xml中增加下述ContextListener即可:

  <web-app>

  org.springframework.web.context.request.RequestContextListener

  ...

  <listener>

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

  </listener>

  ...

  </web-app>

  

 -->

 <!-- OpenSessionInView -->

 <filter>
  <filter-name>OpenSessionInViewFilter</filter-name>
  <filter-class>
       org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
  </filter-class>
  <init-param>
      <param-name>singleSession</param-name>
      <param-value>true</param-value>
  </init-param>
 </filter>

  org.springframework.orm.hibernate3.support.OpenSessionInViewFilter

  延迟加载的实现的原理是cglib动态字节码

  Hibernate对延迟加载的实现原理是CGLIB动态字节码生成技术,即返回的实体并非真正的实体对象,而是经过CGLIB处理后的代理实体,当调用某一未经加载的属性时,代理实体就可以截获这一调用,然后由Hibernate实现动态加载。

  

  如果要使用Hibernate的延迟加载特性,则渲染视图阶段不能关闭事务,因此,事务的范围变为整个HTTP请求的周期。

  

  采用OpenSessionInView模式可以将事务范围界定在请求开始和渲染视图结束后,使得Hibernate的Session在视图渲染时仍有效。有两种方式实现OpenSessionInView模式,一种是使用Spring提供的OpenSessionInViewInterceptor,如果采用Spring MVC框架,可以将这个Interceptor加入到Controller的拦截器链中,事务在Controller处理前开始,在视图渲染后结束,如图11-17所示。

  

  

  

  如果Web层没有采用Spring的MVC框架,而是使用Struts等其他MVC框架,甚至没有使用MVC框架,此时,就无法定义Interceptor,只能采用Filter来实现OpenSessionInView模式。

  

  OpenSessionInViewFilter是Spring提供的一个Filter。在OpenSessionInViewFilter模式下,所有的HTTP请求都将被OpenSessionInViewFilter截获,事务在请求处理前开始,在请求处理完毕后结束,而不管采用何种MVC框架,甚至直接使用JSP,如图11-18所示。

  

  org.springframework.orm.hibernate3.support.OpenSessionInViewFilter(延迟加载,)

  两种方式各有优劣。OpenSessionInViewInterceptor只能用于Spring MVC,但是配置简单,无须过滤URL;OpenSessionInViewFilter适用范围更广,但是必须手动配置web.xml文件,并且必须正确过滤URL。

  

  无论如何,采用以上两种方式的目的都是为了使用Hibernate的延迟加载特性。由于事务也是一种数据库资源,事务持续的时间越久,数据库资源被锁定也越久,应用程序的吞吐量就会降低。因此,要尽量将事务限定在最小的范围内

  

 -->

 <!-- session的过滤控制用户在登录时的权限限制-->

 <filter>
  <filter-name>authorizen</filter-name>
  <filter-class>
      org.springframework.web.filter.DelegatingFilterProxy
  </filter-class>
  <init-param>
      <param-name>targetFilterLifecycle</param-name>
       <param-value>true</param-value>
  </init-param>
 </filter>

 <!-- 以前学习框架经常做登录页面的demo,输入正确的id+pwd就返回成功了。。可是这种模式无法阻止通过URL直接访问其他的页面,在一个非玩具系统中,控制未登录用户的页面访问权限是一个基本功能。

  

  从实现思路讲,验证一个用户的有效登录,大多采用的是登入时候向Session写一个User认证信息,然后在访问每个页面前来判断Session中是否有认证信息。

  

  if(session.get("User")==null)

  另外有很多网站提供记住登陆信息xx天,这种是结合了Cookie的认证信息存储。谈到这里,也可以仔细想想Cookie和Session的作用。比如卓越的购物车就是Cookie做的(因为关闭IE后再访问购物车还记得你的物品),而大多数群集Web服务器的信息也是采用Cookie(因为群集的Session同步开销很大),掌握了Cookie和Session的基本特性,这些都是理所当然的做法了。

  

  一。下面说第一种拦截实现:基于javax.servlet.Filter

  

  1.首先需要到web.xml注册一个filter

  

  (这里是将authorityFilter这个类委托给spring来代理)

  

 -->

 <filter-mapping>
     <filter-name>OpenSessionInViewFilter</filter-name>
     <url-pattern>/*</url-pattern>
 </filter-mapping>
 <filter-mapping>
     <filter-name>authorizen</filter-name>
     <url-pattern>*.do</url-pattern>
 </filter-mapping>
 <!-- 设置监听 -->
 <listener>
  <listener-class>
      org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

 <!-- 今天有一个朋友问了我一个问题,他使用的是Hibernate/Spring/Struts架构,配置使用Spring的OpenSessionInView Filter,但是发现不生效,lazy的集合属性在页面访问的时候仍然报session已经关闭的错误。我和他一起检查了所有的配置和相关的代码,但是没有发现任何问题。经过调试发现,应用程序使用的Session和OpenSessionInView Filter打开的Session不是同一个,所以OpenSessionInView模式没有生效,但是为什么他们不使用同一个Session呢?

  

  检查了一遍Spring的相关源代码,发现了问题的根源:

  

  通常在Web应用中初始化Spring的配置,我们会在web.xml里面配置一个Listener,即:

  

  Xml代码  -->

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

 <!-- 启动spring的一种模式,运行之后要去找上面的<context-param></context-param> -->

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

 <!-- 如果使用Struts,那么需要在Struts的配置文件struts-config.xml里面配置一个Spring的plugin:ContextLoaderPlugIn。

  

  实际上ContextLoaderListener和ContextLoaderPlugIn的功能是重叠的,他们都是进行Spring配置的初始化工作的。因此,如果你不打算使用OpenSessionInView,那么你并不需要在web.xml里面配置ContextLoaderListener。

  

  好了,但是你现在既需要Struts集成Spring,又需要OpenSessionInView模式,问题就来了!

  

  由于ContextLoaderListener和ContextLoaderPlugIn功能重叠,都是初始化Spring,你不应该进行两次初始化,所以你不应该同时使用这两者,只能选择一个,因为你现在需要集成Struts,所以你只能使用ContextLoaderPlugIn。

  

  但是令人困惑的是,ContextLoaderListener和ContextLoaderPlugIn有一个非常矛盾的地方!

  

  ContextLoaderListener初始化spring配置,然后把它放在ServletContext对象里面保存:

  

 servletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); 

  

  请注意,保存的对象的key是WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE!

  

  但是ContextLoaderPlugIn初始化spring配置,然后把它放在ServletContext对象里面保存:

  

  String attrName = getServletContextAttributeName();
  getServletContext().setAttribute(attrName, wac); 

  

  这个attrName和WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE名字是不一样的!

  

  如果仅仅是名字不一样,问题还不大,你仍然可以放心使用ContextLoaderPlugIn,但是当你使用OpenSessionInView的时候,OpenSessionInViewFilter是使用哪个key取得spring配置的呢?

  

WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

  

  显然,OpenSessionInViewFilter是按照WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个key去拿spring配置的!

  

  我们整理一下思路:

  

  ContextLoaderPlugIn保存spring配置的名字叫做attrName;

  ,ContextLoaderListener保存spring配置的名字叫做WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;

  而OpenSessionInView是按照WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个名字去取得spring配置的!

  而你的应用程序却是按照attrName去取得spring的配置的!

  

  所以,OpenSessionInView模式失效!

  

  解决办法:

  修改ContextLoaderPlugIn代码,在getServletContext().setAttribute(attrName, wac);这个地方加上一行代码:

  getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);

  

  或者修改OpenSessionInViewFilter,让它按照attrName去取得spring配置。

 -->

 <!-- 初始页面 -->

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

猜你喜欢

转载自username2.iteye.com/blog/1821912