小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Webwork2之Action Result Type

 Joshua 2006-01-09
Webwork2 之 Action Result Type

      和Webwork1.x不同,Webwork2的Action執(zhí)行完后,其Result對(duì)應(yīng)一個(gè) Result Type,而這個(gè)Result Type完全可以根據(jù)具體應(yīng)用或環(huán)境自己進(jìn)行 定義,只需實(shí)現(xiàn)com.opensymphony.xwork.Result接口。Result Type使得Action的執(zhí)行結(jié)果表現(xiàn)形式可以靈活多變!下面這會(huì)舉例說(shuō)明,這里先看看Webwork2提供的幾種Result Type的定義,該定義在webwork-default.xml中,xwork.xml文件包含了該文件,自定義的Result Type可以直接寫(xiě)在 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>
  其大多都有l(wèi)ocation和parse兩個(gè)參數(shù),location指明action執(zhí)行后接著去哪里,parse指明是否對(duì)location進(jìn)行OGNL表達(dá)式解析。
 
  1) dispatcher
    action執(zhí)行完后,請(qǐng)求會(huì)導(dǎo)向?qū)?yīng)的View,Webwork2幕后其實(shí)是用RequestDispatcher來(lái)處理的,所以原Request/Response對(duì)象會(huì)接著傳遞,原Request中的Atrributes不會(huì)丟失,這點(diǎn)與下面的redirect是不同的。
 
  2) redirect
    對(duì)上次的響應(yīng)將重定向到指定的位置,redirect是重新產(chǎn)生一個(gè)新的Request,原來(lái)Request保存的東西將不再有效,比如不能通過(guò)requet.getAtrribute 取得原來(lái)set的對(duì)象,也不能取得action的實(shí)例,errors,field errors等,因?yàn)锳ction是建立在Single-thread model上的。

  3) chain
    action鏈,特殊的View調(diào)用方式,一個(gè)action執(zhí)行完接著調(diào)用另一個(gè)action。有個(gè)必須的參數(shù)actionName,指明緊接著調(diào)用的另一action對(duì)象。如:
    <result name="success" type="chain">
        <param name="actionName">bar</param>
        <param name="namespace">/foo</param>
    </result>
  執(zhí)行后接著調(diào)用下面的action:
    <action name="bar" class="myPackage.barAction">
        ...
    </action>

  4) velocity
  5) freemarker
  6) jasperreports
  7) xslt
    以上都是用不同技術(shù)的產(chǎn)生不同的View。

  下面我舉個(gè)自定義Result Type的示例,假如我有個(gè)Action testSendmail,根據(jù)處理結(jié)果將給指用戶發(fā)送一份email。自定義一個(gè)Result Type,實(shí)現(xiàn)Result接口。
   
    com.mycompany.webwork.example.SendmailResult
    有三個(gè)必須參數(shù):from ,to, subject,一個(gè)可選參數(shù) 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 實(shí)現(xiàn)Email發(fā)送部分
    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;
    }
  }

  寫(xiě)個(gè)簡(jiǎn)單的測(cè)試Action:
  package com.opensymphony.webwork.example;
 
  import com.opensymphony.xwork.ActionSupport;

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

  測(cè)試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>


  打開(kāi)http://localhost:8080/webwork-example/testsendmail.jsp,提交頁(yè)面,控制臺(tái)輸出:

sending mail....
      To:user@sina.com
    From:root@sina.com
  Subject:hello,webwork

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多