xwork学习(一)
基本上是从网上修改的一个demo
流程是 请求-》拦截-》结果的一个响应。
xml配置
<?xml version="1.0" encoding="UTF-8"?><!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="myPackage" namespace="/helloWorld"><result-types><result-type name="myConsole" name="code">package com.stx.action;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionContext;public class ActionTest implements Action{@Overridepublic String execute() throws Exception {System.out.println("action="+ActionContext.getContext().get("name"));return SUCCESS;}}package com.stx.interceptor;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class InterceptorTest extends AbstractInterceptor{@Overridepublic String intercept(ActionInvocation invocation) throws Exception {System.out.println("intercept:"+ActionContext.getContext().get("name")); //返回值invocation.invoke();//中止整个执行,直接返回一个字符串作为resultCode //通过递归调用负责调用堆栈中下一个Interceptor的执行invocation.invoke();//这个参数其实是负责调度到action的类本身参数传进来 形成一个递归//如果在堆栈内已经不存在任何的Interceptor,调用Action return null;}}package com.stx.result;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.Result;public class ResultTest implements Result {@Overridepublic void execute(ActionInvocation arg0) throws Exception { System.out.println("result execute:"+ ActionContext.getContext().get("name")); }}package com.stx.test;import java.util.HashMap;import java.util.Map;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.inject.Container;public class TestXwork {public static void main(String[] args) {ConfigurationManager cm=new ConfigurationManager ()Configuration cg=cm.getConfiguration(); Container c = cg.getContainer(); ActionProxyFactory actionProxyFactory = c.getInstance(ActionProxyFactory.class); Map<String,Object> m = new HashMap<String,Object>(); m.put("name","bobo"); ActionProxy actionProxy = actionProxyFactory.createActionProxy( "/helloWorld", "actionTest",null, m); for(int i=-2;i<actionProxy.getInvocation().getStack().size();i++){ System.out.println(actionProxy.getInvocation().getStack().pop()); } try { actionProxy.execute(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }