OPEN SESSION IN VIEW配置

使用Hibernate/Spring/Struts架构,配置使用Spring的OpenSessionInView Filter,可是发现不生效,lazy的集合属性在页面访问的时候仍然报session已经关闭的错误。我和他一块儿检查了全部的配置和相关的代码,可是没有发现任何问题。通过调试发现,应用程序使用的Session和OpenSessionInView Filter打开的Session不是同一个,因此OpenSessionInView模式没有生效,可是为何他们不使用同一个Session呢? 
检查了一遍Spring的相关源代码,发现了问题的根源: 
一般在Web应用中初始化Spring的配置,咱们会在web.xml里面配置一个Listener,即:web

Xml代码
<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有一个很是矛盾的地方! 
ContextLoaderPlugIn保存spring配置的名字叫作attrName; 
,ContextLoaderListener保存spring配置的名字叫作WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE; 
而OpenSessionInView是按照WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个名字去取得spring配置的! 
而你的应用程序倒是按照attrName去取得spring的配置的! 
因此,OpenSessionInView模式失效!
spring

 操做步鄹以下:
1.struts-config.xmlsession

删掉加载插件
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
 <set-property property="contextConfigLocation" value="classpath:applicationContext.xml"/>
</plug-in>
2。web.xml
在web.xml文件中新增以下配置架构

注意listener必定要写在最前面 否则、、、、、app

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
<filter>
  <filter-name>opensession</filter-name>
  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>opensession</filter-name>
  <url-pattern>*.do</url-pattern>
 </filter-mapping>
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring.xml</param-value>
 </context-param>
配好后应该就Ok了。。。url

相关文章
相关标签/搜索