struts2、spring和hibernate整合时遇到的一些问题
1.Action对象的产生问题(struts2本身产生好呢?还是让spring代替struts2产生好呢?)
答:这个问题是关于struts2和spring的整合问题,需要用到struts2的struts2-spring-plugin.jar包
如果让struts2本身产生action,action中的set方法struts2会根据这个jar包自动注入(不需要通过@Resource手动注入),如果在set方法上通过@Resource手动注入的话,会起不到任何作用,除非在set方法对应的成员变量上通过@Resource手动注入。
如果让spring代替struts2产生action,这个时候就需要在set方法上通过@Resource手动注入,并且action本身需要注入@Component("xxx")和@Scope("prototype"),注意,这里的xxx需要和struts.xml中action标签的class属性名一致,这里如果没有@Scope("prototype"),那么产生的action就是单例了。
2.jsp页面中文数据传递到action中的乱码问题?
答:struts2.1.8已经修正,只需要改struts.i18n.encoding的值为GBK即可。
如果是以前的版本,需要在web.xml中配置spring中的CharacterEncodingFilter过滤器设置encoding的值为GBK。
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.在jsp页面获取后台load查询出来的数据时,怎么延迟session的关闭时间?
答:在web.xml中配置spring中的OpenSessionInViewFilter过滤器。
4.配置的OpenSessionInViewFilter过滤器是放到struts2配置的过滤器前面还是后面?
答:放到struts2配置的过滤器前面。
5.配置OpenSessionInViewFilter过滤器后,如果spring配置文件中的SessionFactory的名称不是sessionFactory,会出现什么问题,怎么解决?
答:出现异常信息如下:org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'sessionFactory' is defined
需要设置sessionFactoryBeanName的值为spring配置文件中的SessionFactory的名称。
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>mySessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6.配置OpenSessionInViewFilter过滤器后,如果spring配置文件中没有配置aop的事务边界,会出现什么情况?
答:会出现读取数据库中的数据正常,更新(新增、修改、删除) 数据库中的数据异常。异常信息如下:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
Spring MVC远优于structs,任重道远,望自珍重。