解决hibernate 设置为lazy=“true”的集合,通过struts1 访问的时候报session已经关闭的有关问题
解决hibernate 设置为lazy=“true”的集合,通过struts1 访问的时候报session已经关闭的问题
在使用struts1 + hibernate(Struts2没有这问题)过程中,经常要访问lazy="true"的集合类型属性,可是这时候报session已经关闭的现象。
要解决这个问题我们要分析OpenSessionInViewFilter这个类。 OpenSessionInViewFilter是按照WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个key去获取spring配置信息的! 所以,我们在使用过程中配置OpenSessionInViewFilter就不起作用。
解决方法: 修改ContextLoaderPlugIn代码,在getServletContext().setAttribute(attrName, wac);这个地方加上一行代码: getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
或者
修改OpenSessionInViewFilter,让它按照attrName去取得spring配置。
我们按照第一种方法来配置
1.重写struts的插件继承自spring的ContextLoaderPlugIn 的类。
package com.test.plugIn;
import org.springframework.beans.BeansException; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.struts.ContextLoaderPlugIn; public class MyContextLoaderPlugIn extends ContextLoaderPlugIn { protected WebApplicationContext initWebApplicationContext() throws BeansException, IllegalStateException { WebApplicationContext parent = WebApplicationContextUtils .getWebApplicationContext(getServletContext()); WebApplicationContext wac = createWebApplicationContext(parent); String attrName = getServletContextAttributeName(); getServletContext().setAttribute(attrName, wac); getServletContext().setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); return wac; } }
2.配置web.xml文件
<?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" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class> com.navinfo.telematics.roadmap.web.filter.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter> <filter-name>hibernateFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <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</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
3.更改struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> ........ </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> ....... </action-mappings> <message-resources parameter="i18n.ApplicationResources" /> <plug-in className="com.test.plugIn.MyContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/classes/spring/applicationContext.xml" /> </plug-in> </struts-config>