Struts2學習筆記(四):result配置的各種視圖轉寄類型

來源:互聯網
上載者:User
        <action name="helloworld" class="com.jim.action.HelloWorldAction" method="execute" >
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>

result配置類似於struts1中的forward,但struts2中提供了多種結果類型,常用的類型有: dispatcher(預設值)、 redirect 、 redirectAction 、 plainText。

 

重新導向舉例:

      <action name="redirect">
<result type="redirect">/index.jsp</result>
</action>

注意:不能重新導向到web-inf目錄下的頁面

在result中還可以使用${屬性名稱}運算式訪問action中的屬性,運算式裡的屬性名稱對應action中的屬性。如下:

<result type="redirect">/view.jsp?id=${id}</result>

 

下面是redirectAction 結果類型的例子,如果重新導向的action中同一個包下:

<result type="redirectAction">helloworld</result>
 

如果重新導向的action在別的命名空間下:

<result type="redirectAction">
<param name="actionName">helloworld</param>
<param name="namespace">/test</param>
</result>
 plaintext:顯示原始檔案內容,例如:當我們需要原樣顯示jsp檔案原始碼 的時候,我們可以使用此類型。
<result name="source" type="plainText ">
<param name="location">/xxx.jsp</param>
<param name="charSet">UTF-8</param><!-- 指定讀取檔案的編碼 -->
</result>

 

當多個action中都使用到了相同視圖,這時我們應該把result定義為全域視圖。struts1中提供了全域forward,struts2中也提供了相似功能:

<package ....>
<global-results>
<result name="message">/message.jsp</result>
</global-results>
</package>

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.