Spring+struts2 注解action

开始试用Spring+struts2 注解action 的功能惊奇的发现,spring的注解居然支持继承
废话少说,上例子
所有action的超类

package com.example.actions;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.example.ActionTool;
import com.opensymphony.xwork2.ActionSupport;

@Component
@Scope("prototype")
public class AbstractAction extends ActionSupport {
protected static final Log log = LogFactory.getLog(AbstractAction.class);
private ActionTool actionTool;
public ActionTool getActionTool() {
return actionTool;
}
@Resource(name="actionTool")
public void setActionTool(ActionTool actionTool) {
this.actionTool = actionTool;
}

public String index() throws Exception{
throw new Exception("请覆盖 Action 父类方法");

}

}



package com.example.actions;

import javax.annotation.Resource;

import com.example.ActionTool;


public class HelloWorld extends AbstractAction {
private int i;
private String message;
private ActionTool atool;

public String getMessage() {
return message;
}

public String index(){
this.getActionTool().testTool();
this.getAtool().testTool();
log.info(i++);
message = "Hello World!";
return SUCCESS;
}

//@Action("/myword/say")
public String execute() {
message = "Hello World!";
return SUCCESS;
}


public ActionTool getAtool() {
return atool;
}

@Resource(name="actionTool")
public void setAtool(ActionTool atool) {
this.atool = atool;
}

}



package com.example;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

@Component("actionTool")
public class ActionTool {
protected static final Log log = LogFactory.getLog(ActionTool.class);
public void testTool(){


log.info("我是工具类");
}
}


在浏览器中键入 http://localhost:8080/iweb/hello-world
后台打印出
[quote]
[Line : 45 ]: Could not find property [struts.valueStack] -[com.opensymphony.xwork2.util.logging.commons.CommonsLogger.warn(CommonsLogger.java:45)]-2009-02-04 13:08:32,343
[Line : 13 ]: 我是工具类 -[com.example.ActionTool.testTool(ActionTool.java:13)]-2009-02-04 13:08:32,343
[Line : 13 ]: 我是工具类 -[com.example.ActionTool.testTool(ActionTool.java:13)]-2009-02-04 13:08:32,343
[Line : 21 ]: 0 -[com.example.actions.HelloWorld.index(HelloWorld.java:21)]-2009-02-04 13:08:32,343
[Line : 45 ]: Could not find property [org.apache.catalina.jsp_file] -[com.opensymphony.xwork2.util.logging.commons.CommonsLogger.warn(CommonsLogger.java:45)]-2009-02-04 13:08:32,375
[Line : 45 ]: Could not find property [struts.valueStack] -[com.opensymphony.xwork2.util.logging.commons.CommonsLogger.warn(CommonsLogger.java:45)]-2009-02-04 13:08:32,953
[Line : 13 ]: 我是工具类 -[com.example.ActionTool.testTool(ActionTool.java:13)]-2009-02-04 13:08:32,953
[Line : 13 ]: 我是工具类 -[com.example.ActionTool.testTool(ActionTool.java:13)]-2009-02-04 13:08:32,953
[Line : 21 ]: 0 -[com.example.actions.HelloWorld.index(HelloWorld.java:21)]-2009-02-04 13:08:32,953
[Line : 45 ]: Could not find property [org.apache.catalina.jsp_file] -[com.opensymphony.xwork2.util.logging.commons.CommonsLogger.warn(CommonsLogger.java:45)]-2009-02-04 13:08:32,953
[/quote]
很明显,actionTool 被注入了,实例也是prototype的,每次i都是新实例变量,太方便了,以后再也不用在xml中两边都配置action实例了,哇哈哈!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值