XWork2.0 入门实例代码

[b]Xwork.xml 配置文件[/b]

[code]<!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>[/code]

[b]xwork-conversion.properties 属性文件[/b]

[code]com.jamesby.xwork.helloworld.TestType=com.jamesby.xwork.helloworld.TestConvert[/code]

[b]Action 文件和测试Chain的Action 文件[/b]

[code]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;
}
}[/code]

[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;
}
}[/code]

[b]ModelDriven 的模型代码[/b]

[code]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;
}
}[/code]

[b]Result代码[/b]

[code]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"));

}
}[/code]

[b]TypeConvert 文件代码[/b]

[code]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;
}
}[/code]

[b]自定义Interceptor 和 PreResultListener[/b]

[code]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();
}
}[/code]

[b]Main应用程序代码[/b]

[code]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());
}
}
}[/code]

[color=red]刚刚学习Xwork2,本来想学struts2 发现需要xwork基础,个人认为学习xwork一是理解它的拦截器机制,二是理解它的ValueStack机制,准备写个xwork2学习小结,希望大家多指点。[/color]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值