xwork入门

 首先我们自己编写一个Result
  1. package xwork;
  2. import com.opensymphony.xwork2.ActionInvocation;
  3. import com.opensymphony.xwork2.Result;
  4. import com.opensymphony.xwork2.util.TextParseUtil;
  5. /**
  6.  * @author wangmingjie
  7.  * @date 2008-9-29下午10:37:58
  8.  */
  9. public class PrintToConsoleResult implements Result {
  10.     /**
  11.      * 
  12.      */
  13.     private static final long serialVersionUID = 2324715560445939880L;
  14.     private String param ;
  15.     public String getParam() {
  16.         return param;
  17.     }
  18.     public void setParam(String param) {
  19.         this.param = param;
  20.     }
  21.     public void execute(ActionInvocation invocation) throws Exception {
  22.        String result = TextParseUtil.translateVariables(param, invocation.getStack());  
  23.        System.out.println("${test}PrintToConsoleResult输出的:"+result);  
  24.     }
  25. }

 

  1. package xwork;
  2. import com.opensymphony.xwork2.ActionSupport;
  3. /**
  4.  * @author wangmingjie
  5.  * @date 2008-9-29下午10:35:15
  6.  */
  7. public class HelloWorldAction extends ActionSupport{
  8.     /**
  9.      * 
  10.      */
  11.     private static final long serialVersionUID = 2542729556059532666L;
  12.     private String name;
  13.     private int age ;
  14.     private String test = "";
  15.     public String execute() throws Exception {
  16.         System.out.println("action输出的:hello world !");
  17.         test = "hello world test";
  18.         return SUCCESS;
  19.     }
  20.     public String getTest() {
  21.         return test;
  22.     }
  23.     public void setTest(String test) {
  24.         this.test = test;
  25.     }
  26.     
  27.     public void validate(){
  28.         System.out.println("执行校验");
  29.     }
  30.     
  31.     public String create() throws Exception{
  32.         System.out.println("执行了create方法");
  33.         return SUCCESS;
  34.     }
  35.     
  36.     
  37.     public String getName() {
  38.         return name;
  39.     }
  40.     public void setName(String name) {
  41.         this.name = name;
  42.     }
  43.     public int getAge() {
  44.         return age;
  45.     }
  46.     public void setAge(int age) {
  47.         this.age = age;
  48.     }
  49. }

 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE xwork PUBLIC 
  3.     "-//OpenSymphony Group//XWork 1.1.1//EN"
  4.     "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
  5. <xwork>
  6.     <include file="xwork-default.xml" />
  7.     <package name="default-hello-world" extends="xwork-default" namespace="/helloWorld">
  8.         <result-types>
  9.             <result-type name="printToConsole" class="xwork.PrintToConsoleResult" />
  10.         </result-types>
  11.         
  12.         <action name="helloWorld" class="xwork.HelloWorldAction">
  13.             <result type="printToConsole">
  14.                 <param name="param">${test}</param>
  15.             </result>
  16.         </action>
  17.       
  18.     </package>
  19. </xwork>

 

  1. package xwork;
  2. /**
  3.  * @author wangmingjie
  4.  * @date 2008-9-29下午10:41:41
  5.  */
  6. import java.util.LinkedHashMap;
  7. import com.opensymphony.xwork2.ActionProxy;
  8. import com.opensymphony.xwork2.ActionProxyFactory;
  9. import com.opensymphony.xwork2.config.Configuration;
  10. import com.opensymphony.xwork2.config.ConfigurationManager;
  11. import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
  12. import com.opensymphony.xwork2.validator.*;
  13. /**
  14.  * 
  15.  * @author tm_jee
  16.  * @version $Date$ $Id$
  17.  */
  18. public class HelloWorldTutorial {
  19.     public static void main(String[] args) throws Exception {
  20.         ConfigurationManager confManager = new ConfigurationManager();//初始化配置管理类示例
  21.         confManager.addConfigurationProvider(
  22.                 new XmlConfigurationProvider("xwork/xwork_helloworld.xml"true));//通过xml来添加配置文件
  23.         
  24.         Configuration conf = confManager.getConfiguration();
  25.         ActionProxyFactory actionProxyFactory = conf.getContainer().getInstance(ActionProxyFactory.class);
  26.         ActionProxy actionProxy = actionProxyFactory.createActionProxy(
  27.                 "/helloWorld""helloWorld"new LinkedHashMap());
  28.         String result = actionProxy.execute();//action代理执行
  29.         if ("success".equals(result)){
  30.             HelloWorldAction action = (HelloWorldAction) actionProxy.getAction();
  31.        
  32.            // return info back to caller or just print to screen for this example
  33.            System.out.println("action 执行之后:"+action.getTest());
  34.         } else {
  35.            throw new RuntimeException("Im lazy");
  36.         }
  37.         
  38.     }
  39.  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值