最好的解决办法是第2种,这种相当于是把事务从业务层升级到了Action类,不过@Transactional注解还是需要加载业务类上面,不能加到Action类上面,另外OpenSessionInViewFilter过滤器需要放置在Struts的过滤器的上面,如果位置放置反了,还是会出现no session异常,具体放置方法如下图所示:
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>