码迷,mamicode.com
首页 > 其他好文 > 详细

XWork2.0 入门实例代码

时间:2015-05-29 00:49:30      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

Xwork.xml配置文件

技术分享
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 2.0//EN" "http://www.opensymphony.com/xwork/xwork-2.0.dtd">  
<xwork>  
    <include file="xwork-default.xml"/>  
    <package name="default" extends="xwork-default" namespace="/test">  
        <result-types>  
            <result-type name="successResult" class="com.jamesby.xwork.helloworld.SuccessResult" />  
            <result-type name="failedResult" class="com.jamesby.xwork.helloworld.FailedResult" />  
        </result-types>      
        <interceptors>  
            <interceptor name="testInterceptor"  class="com.jamesby.xwork.helloworld.TestInterceptor"/>  
        </interceptors>     
       <action name="testAction" class="com.jamesby.xwork.helloworld.TestAction">  
            <result name="success" type="chain">  
                <param name="actionName">testChainAction</param>  
            </result>   
            <result name="failed" type="failedResult"/>             
            <interceptor-ref name="params"/>  
            <!--Model Driven-->  
            <interceptor-ref name="model-driven"/>  
            <interceptor-ref name="defaultStack"/>  
              
            <interceptor-ref name="testInterceptor"/>           
       </action>  
         
       <action name="testChainAction" class="com.jamesby.xwork.helloworld.TestChainAction">  
            <result name="success" type="successResult">  
                <param name="param">Custom Type</param>  
            </result>   
  
            <result name="failed" type="failedResult"/>             
  
            <interceptor-ref name="params"/>  
            <interceptor-ref name="model-driven"/>  
            <interceptor-ref name="defaultStack"/>  
            <interceptor-ref name="testInterceptor"/>  
  
       </action>       
    </package>  
    <constant name="devMode" value="false" />  
</xwork>  
Xwork.xml配置文件

xwork-conversion.properties 属性文件 

com.jamesby.xwork.helloworld.TestType=com.jamesby.xwork.helloworld.TestConvert  

Action 文件和测试Chain的Action 文件 

技术分享
public class TestAction implements Action, ModelDriven {  
    TestModel book = new TestModel();  
    int id;  
    public String execute() throws Exception {  
  
        System.out.println("\nAction is invoked............");  
        System.out.println("Action:Received Id is " + id);  
        System.out.println("Action:Receive Book Title is " + book.getTitle());  
  
        // book = bookDAO.findById(id, Book.class);  
        book.setTitle("Action Title");  
        if (id < 1000)  
            return "failed";  
        return "success";  
    }  
    public TestModel getModel() {  
        return book;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
}  
View Code
技术分享
public class TestChainAction implements Action, ModelDriven {  
    TestModel book = new TestModel();  
    int id;  
    public String execute() throws Exception {  
  
        System.out.println("\nChain Action is invoked............");  
        System.out.println("Chain Action:Received Id is " + id);  
        System.out.println("Chain Action:Receive Book Title is " + book.getTitle());  
  
        // book = bookDAO.findById(id, Book.class);  
        book.setTitle("Chain Action Title");  
        if (id < 2000)  
            return "success";  
        return "failed";  
    }  
    public TestModel getModel() {  
        return book;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
}  
View Code

ModelDriven 的模型代码 

技术分享
public class TestModel {  
    String id;  
    String title;  
    public void setId(String id) {  
        this.id = id;  
    }  
    public void setTitle(String title) {  
        this.title = title;  
    }  
    public String getId() {  
        return this.id;  
    }  
    public String getTitle() {  
        return this.title;  
    }  
}  
View Code

Result代码 

技术分享
public class SuccessResult implements Result {  
        private TestType param;  
        public void setParam(TestType param) {  
            this.param = param;  
        }  
        public void execute(ActionInvocation invocation) {  
            System.out.println("\nSuccessResult is invoked..............");  
            System.out.println("param is "+param.getValue());  
              
            System.out.println("SuccessResult:Received Title is "  
                    + invocation.getStack().findValue("title"));  
        }  
}  
  
  
public class FailedResult implements Result {  
    public void execute(ActionInvocation invocation) {  
        System.out.println("\nFailedResult is invoked..............");  
        System.out.println("FailedResult:Received Title is "  
                + invocation.getStack().findValue("title"));  
  
    }  
}  
View Code

TypeConvert 文件代码 

技术分享
import java.lang.reflect.Member;  
import java.util.Map;  
  
import ognl.DefaultTypeConverter;  
  
public class TestConvert extends DefaultTypeConverter {  
    public Object convertValue(Map context, Object target, Member member,  
            String propertyName, Object value, Class toType) {  
        return new TestType("" + value);  
    }  
}  
  
  
public class TestType {  
    private String value;  
    public String getValue() {  
        return value;  
    }  
    public void setValue(String value) {  
        this.value = value;  
    }  
    public TestType(String value) {  
        this.value = value;  
    }  
}  
View Code

自定义Interceptor 和 PreResultListener 

技术分享
public class TestInterceptor extends AbstractInterceptor {  
    public String intercept(ActionInvocation invocation) throws Exception {  
        invocation.addPreResultListener(new PreResultListener() {  
            public void beforeResult(ActionInvocation invocation,  
                    String resultCode) {  
                System.out.println("\nPre Result Listerer is invoked........");  
                  
            }  
        });  
        return invocation.invoke();  
    }  
}  
View Code

Main应用程序代码 

技术分享
public class HelloWorldMain {  
  
    public static void main(String[] args) throws Exception {  
        Map paramMap = new HashMap();  
        /** 
         * 1000 以下 testAction Failed Result 
         * 1000-2000 testAction testChainAction SuccessResult 
         * 2000 以上 testAction testChainAction Failed Result 
         */  
        paramMap.put("id", "500");  
        paramMap.put("title", "param title");  
  
        Map context = new HashMap();  
        context.put(ActionContext.PARAMETERS, paramMap);  
  
        ConfigurationManager cm = new ConfigurationManager();  
        Configuration conf = cm.getConfiguration();  
        Container containter = conf.getContainer();  
        DefaultActionProxyFactory actionProxyFactory = new DefaultActionProxyFactory();  
        actionProxyFactory.setContainer(containter);  
        ActionProxy proxy = actionProxyFactory.createActionProxy("/test",  
                "testAction", context);  
        String result = proxy.execute();  
        if ("success".equals(result)) {  
            TestAction action = (TestAction) proxy.getAction();  
            System.out.println("\nReturn Success.................");  
            System.out.println("Return:Return Title is "+action.getModel().getTitle());  
        }  
        if ("failed".equals(result))  
        {  
            TestAction action = (TestAction) proxy.getAction();  
            System.out.println("\nReturn Failed.................");  
            System.out.println("Return:Return Title is "+action.getModel().getTitle());           
        }  
    }  
}  
View Code

 

XWork2.0 入门实例代码

标签:

原文地址:http://www.cnblogs.com/Crow00/p/4537292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!