我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          Struts2中的Result類型應用(action...etc跳轉(zhuǎn))

          概況

          Result類型 是在Action執(zhí)行完,一個結(jié)果返回后決定發(fā)生什么事的類。開發(fā)者可以自由的根據(jù)他們的應用和環(huán)境的需要創(chuàng)建自己的Result類型。例如在WebWork2中,Servlet和Velocity結(jié)果類型已經(jīng)被創(chuàng)建用來顯示web應用程序的畫面。

          注意: 所有的webwork內(nèi)建的Result類型都實現(xiàn)了com.opensymphony.xwork.Result接口. 這個接口是所有action執(zhí)行結(jié)果的通用接口,不管這個結(jié)果是用來顯示一個網(wǎng)頁還是產(chǎn)生一個email,發(fā)送一個JMS消息,等.

          Result類型配置中定義了一些類,把它們映射為action配置中可以引用的名字. 也就是為這些類創(chuàng)建便于記憶的鍵-值對.

          snippet of webwork-default.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-type name="httpheader" class="com.opensymphony.webwork.dispatcher.HttpHeaderResult"/>
              
          <result-type name="stream" class="com.opensymphony.webwork.dispatcher.StreamResult"/>
              
          <result-type name="plaintext" class="com.opensymphony.webwork.dispatcher.PlainTextResult" />
          </result-types>
          ...
          snippet of your xwork.xml
          
          
          <include file="webwork-default.xml"/>

          <package name="myPackage" extends="default">
            
          <action name="bar" class="myPackage.barAction">
              
          <!-- default result type is "dispatcher" -->
              
          <!-- default result name is "success" -->
              
          <result>foo.jsp</result>
              
          <result name="error">error.jsp</result>
              
          </result>
            
          </action>
          </package>

          Result類型

          Webwork提供了一些com.opensymphony.xwork.Result接口的實現(xiàn)來使你的action可以容易的用戶交互.這些Result類型包括:

          Result定義在xwork xml配置文件(xwork.xml)中的action標簽里。如果location參數(shù)是result標簽的唯一的參數(shù),你可以這樣簡化:

          
          
          <action name="bar" class="myPackage.barAction">
            
          <result name="success" type="dispatcher">
              
          <param name="location">foo.jsp</param>
            
          </result>
          </action>

          或者

          
          
          <action name="bar" class="myPackage.barAction">
            
          <result name="success" type="dispatcher">foo.jsp</result>
          </action>

          如果你擴展了webwork-default.xml, 那么默認的返回類型是"dispatcher". 同樣,如果你沒有指定result的名字,默認將是"success". 就是說你可以如下簡化:

          
          
          <action name="bar" class="myPackage.barAction">
            
          <result>foo.jsp</result>
          </action>

          注意 : Parse屬性允許的location參數(shù)作為表達式.例如你可以這樣用:
          Struts2中從一個Action跳轉(zhuǎn)到另一個action,必須將type="redirect"

          
          
          <result name="success" type="redirect">/displayCart.action?userId=${userId}</result>

          注意 : 你也可以指定全局Result以便在多個action中使用. 當要為很多不同的action添加相同的結(jié)果是這樣會節(jié)省時間. Result標簽和全局Result的更多信息,參見Result配置部分
          原文



          posted on 2007-12-03 23:11 々上善若水々 閱讀(30752) 評論(0)  編輯  收藏 所屬分類: Struts2

          主站蜘蛛池模板: 辛集市| 葵青区| 海兴县| 叶城县| 永寿县| 寻乌县| 桃源县| 长白| 西藏| 克东县| 新建县| 鄱阳县| 中方县| 湖北省| 郯城县| 霍山县| 梁山县| 麻栗坡县| 会理县| 垫江县| 修武县| 三门峡市| 锡林浩特市| 洛隆县| 五河县| 灵丘县| 思茅市| 霍山县| 三原县| 和林格尔县| 洱源县| 高碑店市| 淳化县| 正镶白旗| 苍溪县| 扎囊县| 卓尼县| 尉犁县| 青海省| 台北县| 新民市|