webwork中action result type

和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中:
  <result-types>
    <result-type name="dispatcher" class="com.opensymphony.webwork.dispatcher.ServletDispatcherResult" default="true"/>
    <result-type name="redirect" class="com.opensymphony.webwork.dispatcher.ServletRedirectResult"/>
    <result-type name="velocity" class="com.opensymphony.webwork.dispatcher.VelocityResult"/>
    <result-type name="chain" class="com.opensymphony.xwork.ActionChainResult"/>
    <result-type name="xslt" class="com.opensymphony.webwork.views.xslt.XSLTResult"/>
    <result-type name="jasper" class="com.opensymphony.webwork.views.jasperreports.JasperReportsResult"/>
    <result-type name="freemarker" class="com.opensymphony.webwork.views.freemarker.FreemarkerResult"/>
  </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对象。如:
    <result name="success" type="chain">
        <param name="actionName">bar</param>
        <param name="namespace">/foo</param>
    </result>
  执行后接着调用下面的action:
    <action name="bar" class="myPackage.barAction">
        ...
    </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中定义如下:
  <result-types>
    <result-type name="sendmail" class="com.mycompany.webwork.example.SendmailResult"/>
  </result-types>
 
  action定义:
  <action name="testSendmail" class="com.mycompany.webwork.example.TestSendMailAction">
    <result name="success" type="sendmail">
        <param name="from">root@sina.com</param>
        <param name="to">user@sina.com</param>
        <param name="subject">hello,webwork!</param>
    </result>
    <result name="error" type="dispatcher">
        <param name="location">error.jsp</param>
    </result>
  </action>
 
  SendmailResult.java

  package com.opensymphony.webwork.example;

  import com.opensymphony.xwork.ActionInvocation;
  import com.opensymphony.xwork.Result;

  public class SendmailResult implements Result {
    private String to;
    private String from;
    private String subject;
    private String body;

    public void execute(ActionInvocation invocation) throws Exception {
    //TODO 实现Email发送部分
    System.out.println("sending mail....");
    System.out.println("   To:" + to);
    System.out.println("   From:" + from);
    System.out.println("Subject:" + subject);
    }

    public String getBody() {
    return body;
    }

    public void setBody(String body) {
    this.body = body;
    }

    public String getFrom() {
    return from;
    }

    public void setFrom(String from) {
    this.from = from;
    }

    public String getSubject() {
    return subject;
    }

    public void setSubject(String subject) {
    this.subject = subject;
    }

    public String getTo() {
    return to;
    }

    public void setTo(String to) {
    this.to = to;
    }
  }

  写个简单的测试Action:
  package com.opensymphony.webwork.example;
 
  import com.opensymphony.xwork.ActionSupport;

  public class TestSendMailAction extends ActionSupport {
    public String execute() throws Exception {
      return SUCCESS;
    }
  }

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

  testsendmail.jsp

  <%@ taglib prefix="ww" uri="webwork" %>
  <html>
  <head><title>Test sendmail restul type</title></head>
  <body>
 
  <form action="testSendmail.action">
    <input type="Submit" value="Submit"/>
  </form>
  </body>
  </html>


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

sending mail....
      To:user@sina.com
    From:root@sina.com
  Subject:hello,webwork 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值