Spring+Hibernate中, 如果不用OpenSessionInView模式,session得手动打开和关闭,很是不爽,无疑增加了一些不必要难看的代码,配置OpenSessionInView模式也很简单,Spring提供了两种方式:
1、过滤流Filter
xml 代码
-
<filter>
- <filter-name>hibernateFilterfilter-name>
- <filter-class>
-
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
-
filter-class>
-
filter>
-
- <filter-mapping>
- <filter-name>hibernateFilterfilter-name>
- <url-pattern>*.shtmlurl-pattern>
-
filter-mapping>
2、Interceptor
java 代码
- "simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- "interceptors" ref="openSessionInViewInterceptor"/>
- "mappings">
-
<props></props>
-
。。。
-
props>
-
property>
-
bean>
-
-
- "openSessionInViewInterceptor"
-
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
- "sessionFactory" ref="sessionFactory"/>
-
bean>
推荐用第二种方式
相关讨论:
http://www.iteye.com/topic/24556