Struts2中result類型說明

       Result的首要職責,是封裝Action層到View層的跳轉邏輯。在Action執行完畢之後,框架需要把代碼的執行權重新交還給Web容器,並轉向到相應的頁面或者其他類型的

View層。而這個跳轉邏輯,就由Result來完成。

  View層的顯示類型非常多,有最常見的JSP、當下非常流行的Freemarker/Velocity模板、Redirect到一個新的地址、文本流、圖片流、甚至是JSON對象等等。所以Result

層的獨立存在,就能夠對這些顯示類型進行區分,並封裝合理的跳轉邏輯。

首先,我們打開Struts2-core-2.3.16.jar/struts-default.xml,在這裏面我們可以看到result類型的一些配置信息;

<result-types>
            <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
            <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
            <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
            <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
            <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
            <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
            <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
            <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
            <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
            <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />
        </result-types>

dispatcher:

         dispatcher主要用於返回JSP,HTML等以頁面爲基礎View視圖,這個也是Struts2默認的Result類型。在使用dispatcher時,唯一需要指定的,是JSP或者HTML頁面的位置,這個位置將被用於定位返回的頁面;

<result name="success">/index.jsp</result> 

freemarker / velocity

             隨着模板技術的越來越流行,使用Freemarker或者Velocity模板進行View層展示的開發者越來越多。Struts2同樣爲模板作爲Result做出了支持。由於模板的顯示需要模板(Template)與數據(Model)的緊密配合,所以在Struts2中,這兩個Result的主要工作是爲模板準備數據。


redirect

redirect是重定向到一個URL,如果你需要對目標地址傳遞參數,那麼需要在目標地址url或者配置文件中指出:

  <action name="index2">
        <result type="redirect">/index2.jsp</result>
  </action>
訪問時:http://localhost:8080/Struts2_02/index2,跳轉後的頁面是:http://localhost:8080/Struts2_02/index2.jsp


redirectAction

<action name="financeReport" class="com.mycompany.reports.FinanceReportAction"> 
	<result name="input">/jsp/index.jsp</result>            
	<result name="success" type="redirectAction">
		<param name="actionName">displayReport</param>
		<param name="parse">false</param>
		<param name="anchor">SUMMARY</param>
	</result>
</action>


chain

<package name="public" extends="struts-default">
    <!-- Chain creatAccount to login, using the default parameter -->
    <action name="createAccount" class="...">
        <result type="chain">login</result>
    </action>

    <action name="login" class="...">
        <!-- Chain to another namespace -->
        <result type="chain">
            <param name="actionName">dashboard</param>
            <param name="namespace">/secure</param>
        </result>
    </action>
</package>

<package name="secure" extends="struts-default" namespace="/secure">
    <action name="dashboard" class="...">
        <result>dashboard.jsp</result>
    </action>
</package>

struts2中關於result的返回類型一般我們是轉發到一個jsp頁面或者是html頁面等,但是struts2中的result的返回類型還有redirect,redirectAction,chain

當使用type=“redirectAction” 或type=“redirect”提交到一個action並且需要傳遞一個參數時。這裏是有區別的:

一、使用redirect需要後綴名 使用redirectAction不需要後綴名
二、type="redirect" 的值可以轉到其它命名空間下的action,而redirectAction只能轉到同一命名空下的 action,因此它可以省略.action的後綴直接寫action的名稱。

如:

<result name="success" type="redirect">viewTask.action</result>
<result name="success" type="redirectAction">viewTask</result>

(1)redirect:action處理完後重定向到一個視圖資源(如:jsp頁面),請求參數全部丟失,action處理結果也全部丟失。

(2)redirectAction:action處理完後重定向到一個action,請求參數全部丟失,action處理結果也全部丟失。

(3)chain:action處理完後轉發到一個action,請求參數全部丟失,action處理結果不會丟失。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章