jeffy

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            70 Posts :: 1 Stories :: 14 Comments :: 0 Trackbacks
              和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>

                   (申明:本文來源于網絡,摘錄于此,僅為日后方便查看)
          posted on 2006-03-02 15:45 Live-in Java 閱讀(1762) 評論(0)  編輯  收藏 所屬分類: Web Framwork
          主站蜘蛛池模板: 定南县| 临海市| 武安市| 横峰县| 平阴县| 罗甸县| 周宁县| 阳东县| 大宁县| 三门峡市| 广东省| 万州区| 阿拉善盟| 乌拉特中旗| 淳安县| 缙云县| 武乡县| 黑山县| 彭阳县| 白沙| 丘北县| 阳江市| 伊川县| 新余市| 同德县| 达州市| 白朗县| 穆棱市| 建水县| 兴国县| 南部县| 柳林县| 安溪县| 邢台市| 卢氏县| 清丰县| 广汉市| 天祝| 乌苏市| 大同县| 水城县|