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实例了,哇哈哈!

你可能感兴趣的:(Spring+struts2 注解action)