OpenSessionInViewFilter不起作用

使用Spring Hibernate Struts 编写web应用,搭架构的时候测试出现 could not initialize proxy - no Session,网上搜了下说:是因为,你使用了lazy=true,这样hibernate在从数据库中调数据的时候是不会把关联的对象查出来的,而是保存一个获取值得方法,在你使用getXXX()调用的时候,hiberante会利用这个保存的方法去从数据库中取数据。而往往我们在jsp页面中使用getXXX()准备展示数据的时候,session早已经在dao中就关闭了。

于是就按照网上的方法在web.xml里面加入如下配置:
<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>

配置完成后还是报错,思考了一下,把这个filter放到了struts的StrutsPrepareAndExecuteFilter上面,不报错了。

思考原因:如果是先进入StrutsPrepareAndExecuteFilter,再进入OpenSessionInViewFilter,OpenSessionInViewFilter就管理不了Struts的一些操作了。

猜你喜欢

转载自whoosh.iteye.com/blog/1154924