getCurrentSession()事务会自动关闭,所以在所有jsp页面查询数据都会关闭session。要想在jsp查询数据库需要加入OpenSessionInViewFilter过滤器。
session通常随着事物开始而打开,事物结束而关闭,因此在jsp页面调用数据库时会出现No Session found for current thread错误。
openSessionInView的作用就是扩大事物的边界,由它来控制session的关闭
openSessionInView的配置
web.xml
<!-- openSessionInView过滤器 -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*openSessionInViewFilter要放在Struts2Filter前面