ss2h整合中,出现了懒加载的错误,而且已经配置了openSessionInViewFilter,与ssh(struts1)不同,struts2不能随便配置过滤器,必须要放在指定的范围内
stutr1 中,怎么放基本都可以,但是在struts2中,openSessionInViewFilter过滤器一定要陪在 <filter-name>struts2</filter-name>之中,例如
webxml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 指定spring的配置文件,默认从web根目录下寻找文件,通过从spring提供的classpath:前缀指定从类路径开始寻找 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 即可以简单理解为tomcat初始化spring的applicationContext.xml 并放入类似request.setAttribute(...)中-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring提供的过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 该过滤器作用类似于取消懒加载,会在需要关联时向数据库发送请求 -->
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
注意最下面,<filter-name>openSessionInViewFilter</filter-name> 一定要被 <filter-name>struts2</filter-name>包住!!!否则,以struts1的老方法,可以让你调一壶的。