首先我们自己编写一个Result
- packagexwork;
- importcom.opensymphony.xwork2.ActionInvocation;
- importcom.opensymphony.xwork2.Result;
- importcom.opensymphony.xwork2.util.TextParseUtil;
- /**
- *@authorwangmingjie
- *@date2008-9-29下午10:37:58
- */
- publicclassPrintToConsoleResultimplementsResult{
- /**
- *
- */
- privatestaticfinallongserialVersionUID=2324715560445939880L;
- privateStringparam;
- publicStringgetParam(){
- returnparam;
- }
- publicvoidsetParam(Stringparam){
- this.param=param;
- }
- publicvoidexecute(ActionInvocationinvocation)throwsException{
- Stringresult=TextParseUtil.translateVariables(param,invocation.getStack());
- System.out.println("${test}PrintToConsoleResult输出的:"+result);
- }
- }
- packagexwork;
- importcom.opensymphony.xwork2.ActionSupport;
- /**
- *@authorwangmingjie
- *@date2008-9-29下午10:35:15
- */
- publicclassHelloWorldActionextendsActionSupport{
- /**
- *
- */
- privatestaticfinallongserialVersionUID=2542729556059532666L;
- privateStringname;
- privateintage;
- privateStringtest="";
- publicStringexecute()throwsException{
- System.out.println("action输出的:helloworld!");
- test="helloworldtest";
- returnSUCCESS;
- }
- publicStringgetTest(){
- returntest;
- }
- publicvoidsetTest(Stringtest){
- this.test=test;
- }
- publicvoidvalidate(){
- System.out.println("执行校验");
- }
- publicStringcreate()throwsException{
- System.out.println("执行了create方法");
- returnSUCCESS;
- }
- publicStringgetName(){
- returnname;
- }
- publicvoidsetName(Stringname){
- this.name=name;
- }
- publicintgetAge(){
- returnage;
- }
- publicvoidsetAge(intage){
- this.age=age;
- }
- }
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPExworkPUBLIC
- "-//OpenSymphonyGroup//XWork1.1.1//EN"
- "http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
- <xwork>
- <includefile="xwork-default.xml"/>
- <packagename="default-hello-world"extends="xwork-default"namespace="/helloWorld">
- <result-types>
- <result-typename="printToConsole"class="xwork.PrintToConsoleResult"/>
- </result-types>
- <actionname="helloWorld"class="xwork.HelloWorldAction">
- <resulttype="printToConsole">
- <paramname="param">${test}</param>
- </result>
- </action>
- </package>
- </xwork>
- packagexwork;
- /**
- *@authorwangmingjie
- *@date2008-9-29下午10:41:41
- */
- importjava.util.LinkedHashMap;
- importcom.opensymphony.xwork2.ActionProxy;
- importcom.opensymphony.xwork2.ActionProxyFactory;
- importcom.opensymphony.xwork2.config.Configuration;
- importcom.opensymphony.xwork2.config.ConfigurationManager;
- importcom.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
- importcom.opensymphony.xwork2.validator.*;
- /**
- *
- *@authortm_jee
- *@version$Date$$Id$
- */
- publicclassHelloWorldTutorial{
- publicstaticvoidmain(String[]args)throwsException{
- ConfigurationManagerconfManager=newConfigurationManager();//初始化配置管理类示例
- confManager.addConfigurationProvider(
- newXmlConfigurationProvider("xwork/xwork_helloworld.xml",true));//通过xml来添加配置文件
- Configurationconf=confManager.getConfiguration();
- ActionProxyFactoryactionProxyFactory=conf.getContainer().getInstance(ActionProxyFactory.class);
- ActionProxyactionProxy=actionProxyFactory.createActionProxy(
- "/helloWorld","helloWorld",newLinkedHashMap());
- Stringresult=actionProxy.execute();//action代理执行
- if("success".equals(result)){
- HelloWorldActionaction=(HelloWorldAction)actionProxy.getAction();
- //returninfobacktocallerorjustprinttoscreenforthisexample
- System.out.println("action执行之后:"+action.getTest());
- }else{
- thrownewRuntimeException("Imlazy");
- }
- }
- }