struts2与spring 结合时一些问题
2012-05-22 18:56
351 查看
1,spring自动生成action 的实例,前提要在web.xml中配置spring的监听器以及加入struts2-spring-plugin.jar包
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
将struts.xml配置的action命名为和Action类名字相同@Component("action命名");@Scope("prototype")不要忘记,每一个产生的action对象都是新的;
其实在action名字相同的情况下上边两个注解可以都不写,默认的scope就是prototype;
2,默认情况下action中的属性再不用注解的情况下都会自动注入,前提是属性名称与类名的@Component("命名")相同,属性名称首字母小写,名称不相同的情况下需要用注解@Resource(name=""),一般常用在setter方法上;
3,异常:could not initialize proxy - no Session...
在web.xml中struts之前配置:
<filter>
<filter-name>opensession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>opensession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这样session一旦打开就会等到我们访问完之后才会关闭,把session打开的时间一直延续到View层。
使用load时需要需要上述操作,使用get时不需要
4,总结spring容器中装的bean是service和dao;action的产生完全由struts2-spring-plugin产生,action类中不需要写注解,属性名称要与类名的@Component("命名")相同;
5,如果在bean.xml中没有配置事务,如果在web.xml配置了OpenSessionInView,则认为事务是只读的(readOnly),则不允许做增删改的操作;
所以正常情况下两者都应该配置;
6,spring中一个解决编码问题的过滤器也很好用,在web.xml中配置
<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>
7,关于spring管理action的官方文档解释:在action类上边配置@Component('"action")@Scope("prototype");在struts.xml配置文件中action一行的class="action"
;这样action就像其他普通类一样被spring管理。当然action的属性都需要@Resource()注解。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
将struts.xml配置的action命名为和Action类名字相同@Component("action命名");@Scope("prototype")不要忘记,每一个产生的action对象都是新的;
其实在action名字相同的情况下上边两个注解可以都不写,默认的scope就是prototype;
2,默认情况下action中的属性再不用注解的情况下都会自动注入,前提是属性名称与类名的@Component("命名")相同,属性名称首字母小写,名称不相同的情况下需要用注解@Resource(name=""),一般常用在setter方法上;
3,异常:could not initialize proxy - no Session...
在web.xml中struts之前配置:
<filter>
<filter-name>opensession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>opensession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这样session一旦打开就会等到我们访问完之后才会关闭,把session打开的时间一直延续到View层。
使用load时需要需要上述操作,使用get时不需要
4,总结spring容器中装的bean是service和dao;action的产生完全由struts2-spring-plugin产生,action类中不需要写注解,属性名称要与类名的@Component("命名")相同;
5,如果在bean.xml中没有配置事务,如果在web.xml配置了OpenSessionInView,则认为事务是只读的(readOnly),则不允许做增删改的操作;
所以正常情况下两者都应该配置;
6,spring中一个解决编码问题的过滤器也很好用,在web.xml中配置
<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>
7,关于spring管理action的官方文档解释:在action类上边配置@Component('"action")@Scope("prototype");在struts.xml配置文件中action一行的class="action"
;这样action就像其他普通类一样被spring管理。当然action的属性都需要@Resource()注解。
相关文章推荐
- ssh整合出现的一些问题总结(spring4+struts2+hibernate4)
- struts2改spring boot过程中一些问题及解决办法记录
- spring3+struts2环境搭建中遇到的一些问题
- struts2结合spring json序列化问题
- Struts2和Spring整合中的一些问题
- Struts2中(动态方法调用)DMI的一些问题
- 解决json结合struts2时,Class org.apache.struts2.json.JSONWriter can not access a member of * 的问题
- java的一些问题:spring
- 由Spring管理的Struts2 fielderror信息重复累加问题
- spring整合struts2遇到的问题一:scope设置
- struts2,hibernate4,spring3配置时问题汇总及解决办法
- 关于Spring一些小问题
- 【笔记】struts2学习笔记(二)--常见问题/一些配置/Myeclipse使用
- spring整合quartz备份数据库的一些问题
- 关于struts2的报告错误,spring注入问题,今天调式网站时直接出现了500错误,强悍的500
- SpringBoot+thymeleaf项目页面的一些问题
- struts2的一些问题
- struts2,hibernate4,spring3配置时问题汇总及解决办法
- mybatis配置的一些小问题总结 mybatis报错:org.mybatis.spring.MyBatisSystemException: nested exception is org.apach
- 关于struts2和spring security的结合以及spring security传参的问题