和Webwork1.x不同,Webwork2的Action执行完后,其Result对应一个 Result Type,而这个Result Type完全可以根据具体应用或环境自己进行 定义,只需实现com.opensymphony.xwork.Result接口。Result Type使得Action的执行结果表现形式可以灵活多变!下面这会举例说明,这里先看看Webwork2提供的几种Result Type的定义,该定义在webwork-default.xml中,xwork.xml文件包含了该文件,自定义的Result Type可以直接写在 xwork.xml中:
 

 
  
  1. <result-types> 
  2.   <result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/> 
  3.   <result-type name="redirect" class="com.opensymphony.webwork.dispatcher.ServletRedirectResult"/> 
  4.   <result-type name="velocity" class="com.opensymphony.webwork.dispatcher.VelocityResult"/> 
  5.   <result-type name="chain" class="com.opensymphony.xwork.ActionChainResult"/> 
  6.   <result-type name="xslt" class="com.opensymphony.webwork.views.xslt.XSLTResult"/> 
  7.   <result-type name="jasper" class="com.opensymphony.webwork.views.jasperreports.JasperReportsResult"/> 
  8.   <result-type name="freemarker" class="com.opensymphony.webwork.views.freemarker.FreemarkerResult"/> 
  9. </result-types> 


  其大多都有location和parse两个参数,location指明action执行后接着去哪里,parse指明是否对location进行OGNL表达式解析。
 
  1) dispatcher
    action执行完后,请求会导向对应的View,Webwork2幕后其实是用RequestDispatcher来处理的,所以原 Request/Response对象会接着传递,原Request中的Atrributes不会丢失,这点与下面的redirect是不同的。
 
  2) redirect
    对上次的响应将重定向到指定的位置,redirect是重新产生一个新的Request,原来Request保存的东西将不再有效,比如不能通过requet.getAtrribute 取得原来set的对象,也不能取得action的实例,errors,field errors等,因为Action是建立在Single-thread model上的。

  3) chain
    action链,特殊的View调用方式,一个action执行完接着调用另一个action。有个必须的参数actionName,指明紧接着调用的另一action对象。如:
 

 
  
  1. <result name="success" type="chain"> 
  2.     <param name="actionName">bar</param> 
  3.     <param name="namespace">/foo</param> 
  4. </result> 


  执行后接着调用下面的action:
 

 
  
  1. <action name="bar" class="myPackage.barAction">  
  2.     ...  
  3. </action> 

  4) velocity
  5) freemarker
  6) jasperreports
  7) xslt
    以上都是用不同技术的产生不同的View。

  下面我举个自定义Result Type的示例,假如我有个Action testSendmail,根据处理结果将给指用户发送一份email。自定义一个Result Type,实现Result接口。
  
    com.mycompany.webwork.example.SendmailResult
    有三个必须参数:from ,to, subject,一个可选参数 body。
  在xwork.xml中定义如下:
 

 
  
  1. <result-types> 
  2.   <result-type name="sendmail" class="com.mycompany.webwork.example.SendmailResult"/> 
  3. </result-types> 


 
  action定义:
 

 
  
  1. <action name="testSendmail" class="com.mycompany.webwork.example.TestSendMailAction">  
  2.   <result name="success" type="sendmail">  
  3.       <param name="from">root@sina.com</param>  
  4.       <param name="to">user@sina.com</param>  
  5.       <param name="subject">hello,webwork!</param>  
  6.   </result>  
  7.   <result name="error" type="dispatcher">  
  8.       <param name="location">error.jsp</param>  
  9.   </result>  
  10. </action> 


 
  SendmailResult.java

  

 
  
  1. package com.opensymphony.webwork.example;  
  2.  
  3.   import com.opensymphony.xwork.ActionInvocation;  
  4.   import com.opensymphony.xwork.Result;  
  5.  
  6.   public class SendmailResult implements Result {  
  7.     private String to;  
  8.     private String from;  
  9.     private String subject;  
  10.     private String body;  
  11.  
  12.     public void execute(ActionInvocation invocation) throws Exception {  
  13.     //TODO 实现Email发送部分  
  14.     System.out.println("sending mail....");  
  15.     System.out.println("   To:" + to);  
  16.     System.out.println("   From:" + from);  
  17.     System.out.println("Subject:" + subject);  
  18.     }  
  19.  
  20.     public String getBody() {  
  21.     return body;  
  22.     }  
  23.  
  24.     public void setBody(String body) {  
  25.     this.body = body;  
  26.     }  
  27.  
  28.     public String getFrom() {  
  29.     return from;  
  30.     }  
  31.  
  32.     public void setFrom(String from) {  
  33.     this.from = from;  
  34.     }  
  35.  
  36.     public String getSubject() {  
  37.     return subject;  
  38.     }  
  39.  
  40.     public void setSubject(String subject) {  
  41.     this.subject = subject;  
  42.     }  
  43.  
  44.     public String getTo() {  
  45.     return to;  
  46.     }  
  47.  
  48.     public void setTo(String to) {  
  49.     this.to = to;  
  50.     }  
  51.   } 

  写个简单的测试Action:
 

 
  
  1. package com.opensymphony.webwork.example;  
  2.  
  3.  import com.opensymphony.xwork.ActionSupport;  
  4.  
  5.  public class TestSendMailAction extends ActionSupport {  
  6.    public String execute() throws Exception {  
  7.      return SUCCESS;  
  8.    }  
  9.  }  

  测试jsp,把它放在webwork-example/下:

  testsendmail.jsp

  

 
  
  1. <%@ taglib prefix="ww" uri="webwork" %> 
  2.   <html> 
  3.   <head><title>Test sendmail restul type</title></head> 
  4.   <body> 
  5.    
  6.   <form action="testSendmail.action"> 
  7.     <input type="Submit" value="Submit"/> 
  8.   </form> 
  9.   </body> 
  10.   </html> 


  打开http://localhost:8080/webwork-example/testsendmail.jsp,提交页面,控制台输出:

 

 
  
  1. sending mail....  
  2.       To:user@sina.com  
  3.     From:root@sina.com  
  4.   Subject:hello,webwork