首先我们自己编写一个Result
- package xwork;
- import com.opensymphony.xwork2.ActionInvocation;
- import com.opensymphony.xwork2.Result;
- import com.opensymphony.xwork2.util.TextParseUtil;
- /**
- * @author wangmingjie
- * @date 2008-9-29下午10:37:58
- */
- public class PrintToConsoleResult implements Result {
- /**
- *
- */
- private static final long serialVersionUID = 2324715560445939880L;
- private String param ;
- public String getParam() {
- return param;
- }
- public void setParam(String param) {
- this.param = param;
- }
- public void execute(ActionInvocation invocation) throws Exception {
- String result = TextParseUtil.translateVariables(param, invocation.getStack());
- System.out.println("${test}PrintToConsoleResult输出的:"+result);
- }
- }
- package xwork;
- import com.opensymphony.xwork2.ActionSupport;
- /**
- * @author wangmingjie
- * @date 2008-9-29下午10:35:15
- */
- public class HelloWorldAction extends ActionSupport{
- /**
- *
- */
- private static final long serialVersionUID = 2542729556059532666L;
- private String name;
- private int age ;
- private String test = "";
- public String execute() throws Exception {
- System.out.println("action输出的:hello world !");
- test = "hello world test";
- return SUCCESS;
- }
- public String getTest() {
- return test;
- }
- public void setTest(String test) {
- this.test = test;
- }
- public void validate(){
- System.out.println("执行校验");
- }
- public String create() throws Exception{
- System.out.println("执行了create方法");
- return SUCCESS;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE xwork PUBLIC
- "-//OpenSymphony Group//XWork 1.1.1//EN"
- "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
- <xwork>
- <include file="xwork-default.xml" />
- <package name="default-hello-world" extends="xwork-default" namespace="/helloWorld">
- <result-types>
- <result-type name="printToConsole" class="xwork.PrintToConsoleResult" />
- </result-types>
- <action name="helloWorld" class="xwork.HelloWorldAction">
- <result type="printToConsole">
- <param name="param">${test}</param>
- </result>
- </action>
- </package>
- </xwork>
- package xwork;
- /**
- * @author wangmingjie
- * @date 2008-9-29下午10:41:41
- */
- import java.util.LinkedHashMap;
- import com.opensymphony.xwork2.ActionProxy;
- import com.opensymphony.xwork2.ActionProxyFactory;
- import com.opensymphony.xwork2.config.Configuration;
- import com.opensymphony.xwork2.config.ConfigurationManager;
- import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
- import com.opensymphony.xwork2.validator.*;
- /**
- *
- * @author tm_jee
- * @version $Date$ $Id$
- */
- public class HelloWorldTutorial {
- public static void main(String[] args) throws Exception {
- ConfigurationManager confManager = new ConfigurationManager();//初始化配置管理类示例
- confManager.addConfigurationProvider(
- new XmlConfigurationProvider("xwork/xwork_helloworld.xml", true));//通过xml来添加配置文件
- Configuration conf = confManager.getConfiguration();
- ActionProxyFactory actionProxyFactory = conf.getContainer().getInstance(ActionProxyFactory.class);
- ActionProxy actionProxy = actionProxyFactory.createActionProxy(
- "/helloWorld", "helloWorld", new LinkedHashMap());
- String result = actionProxy.execute();//action代理执行
- if ("success".equals(result)){
- HelloWorldAction action = (HelloWorldAction) actionProxy.getAction();
- // return info back to caller or just print to screen for this example
- System.out.println("action 执行之后:"+action.getTest());
- } else {
- throw new RuntimeException("Im lazy");
- }
- }
- }