Struts2组合spring
来源:互联网 发布:linux mv 创建文件夹 编辑:程序博客网 时间:2024/05/29 04:46
Struts integrates spring的好处就是解除 hard-code,如下是Struts官网给出的integrate spring的理由:
- If I need to replace the EditServiceInMemory with another class that implements the EditService interface I'll have to hunt down and replace all statements where I hard-coded the dependency.
- I cannot test EditAction without using the EditServiceInMemory class. I cannot isolate EditAction by using a stub implementation of EditService when writing my test case because the use of EditServiceInMemory is hard-coded.
这个integration主要有两种方式:
第一种、借助struts2-spring-plugin-x.x.x.jar通过spring来管理、生成和autowriting(自动组装)actionsport依赖的类实例。这种方式的action还是Struts自己来管理的,并没有registe到spring中。
要通过spring来autowriting依赖关系,也照样是需要相应的get和set方法的。
public class StrutsSpringTestAction extends ActionSupport {private Struts2SpringTestInterface install;public Struts2SpringTestInterface getInstall() {return install;}public void setInstall(Struts2SpringTestInterface install) {this.install = install;}@Overridepublic String execute() throws Exception {return super.execute();}}
然后在spring的配置文件applicationContext.xml中注册被依赖对象Struts2SpringTestInterface
<bean id="install" class="com.cmpsky.strutstest.server.Struts2SpringTestImpl">
</bean>
注意这里的beanId 为install,这个与Action中的属性install是同名的,spring的默认autowriting就是 by name的方式的(所以它俩的相同)。
由于这里的action并没有托管到spring,所以不需要spring中注册StrutsSpringTestAction 。
在struts.xml中再配置好action就OK了
<action name="struts2spring" class="com.cmpsky.strutstest.action.struts2spring.StrutsSpringTestAction">
<result name="success">/struts2springResult.jsp</result>
</action>
关于spring的autowriting有如下几种方式:
The autowire
property can be set to several options.
name
Auto-wire by matching the name of the bean in Spring with the name of the property in your action. This is the default
type
Auto-wire by looking for a bean registered with Spring of the same type as the property in your action. This requires you to have only one bean of this type registered with Spring
auto
Spring will attempt to auto-detect the best method for auto-wiring your action
constructor
Spring will auto-wire the parameters of the bean's constructor
no
Turn off externally defined autowiring. Annotation-driven injection and injection based on Springs *Aware-interfaces still applies
struts.objectFactory.spring.autoWire = type
还有一种方式就是:把action注册在spring中管理,这就不需要struts2-spring-plugin-x.x.x.jar这个插件了。Struts官网是把这个作为一个alternative来介绍的。
1、在spring的applicationContext.xml中完成如下配置:
<span style="white-space:pre"></span><bean id="install" class="com.cmpsky.strutstest.server.Struts2SpringTestImpl"></bean><bean id="noPlugin" class="com.cmpsky.strutstest.action.struts2spring.StrutsSpringTestAction"><property name="install" ref="install"></property></bean>
<span style="white-space:pre"></span><action name="noplugin" class="noPlugin"> <result name="success">/struts2springResult.jsp</result> </action>注意class="noPlugin"的值和<bean id="noPlugin" 的值都为noPlugin是有映射关系的。
然后就又OK了!
- Struts2组合spring
- Hibernate与Struts2和Spring组合开发
- struts2+spring+dwr+ibatis整合jar组合
- 109---Spring---Struts2---mybatis组合框架
- SSH组合工程之-struts2&hibernate&spring(工程下载)
- spring struts2
- struts2+spring
- spring+struts2
- Spring+Struts2
- Spring+Struts2
- spring/struts2
- spring+struts2
- Spring -- spring整合struts2
- struts2+spring+spring jdbc
- Spring:Spring整合Struts2
- 【Spring】Spring整合Struts2
- spring3.1.1+struts2.3.3组合
- Spring+JDBC组合开发
- ie9 scrollbar中hover 高度增高的bug
- 南邮 OJ 1095 奇特的图形
- Same Tree
- C++实现两个字符串之间的Levenshtein Distance(编辑距离)
- 0-1排序引理
- Struts2组合spring
- 常用做action recognition 的数据集介绍
- linux 下编译安装Qt
- 埃博拉病毒的实验室症断
- 无线调试——不需要root也能wifi调式
- HDU 3879 Base Station(最小割的最大权闭包 最大获利)经典
- My C++ Study Notes
- Function构造函数
- hdu 1358 Period(给定一个字符串,求有多少个前缀(包括自己本身),它是由k(k>2,并且尽量大)个循环节组成的)