- 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");
- }
- }
- }