Struts2组合spring

Struts integrates spring的好处就是解除 hard-code,如下是Struts官网给出的integrate spring的理由:

  1. 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.
  2. 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;
	}

	@Override
	public 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.xml中通过如下的key进行配置的:
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>


2、在Struts的struts.xml中完成如下配置:

<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了!




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值