日期:2014-05-16  浏览次数:20455 次

struts2 + json +hibernate的延迟失效

在项目中使用SSH框架开发时,可以在web.xml中配置session为页面级的,我们可以在页面中获取数据,hibernate的延时加载正常。

?

但是不知道为什么Struts2 + json + hibernate,那个延时加载失效了。

比如如果用session.load(),加载抛异常。只好用session.get了。

?

最后我网上找到解决方案了:

?

第一步:在web.xml配置页面级session(这一步早就完成 )

<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

?

第二步:配置struts.xml中action的配置文件,

????????????? 只需要将.*hibernateLazyInitializer过滤就可以了。

	<package name="money" extends="json-default" namespace="/">
		<action name="user" class="com.wjt276.money.action.UserAction">
			<result name="index">/user/user_list.jsp</result>
			<result type="json" >
				<!--<param name="root">pm</param>-->
				<param name="ignoreHierarchy" >false</param><!-- 设置JSON输出父类中的数据 -->				
				<param name="excludeProperties">.*hibernateLazyInitializer</param><!-- excludeProperties参数过滤不需要输出的数据字段 -->
			</result>
		</action>
	</package>
?

就OK了。

1 楼 bainian_205 2010-12-23  
这个问题已经困惑我两天了,我考虑到是延迟加载的问题了,但没有找到过滤哪个属性,非常非常感谢
2 楼 lintghi 2012-04-12  
<param name="excludeProperties">.*hibernateLazyInitializer</param>的确有用.