标签:
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-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; } }
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; } }
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; } }
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")); } }
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; } }
自定义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(); } }
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()); } } }
标签:
原文地址:http://www.cnblogs.com/Crow00/p/4537292.html