【Struts2學習筆記(2)】Action配置中的各項預設值和Action中result的各種轉寄類型,struts2學習筆記

來源:互聯網
上載者:User

【Struts2學習筆記(2)】Action配置中的各項預設值和Action中result的各種轉寄類型,struts2學習筆記
一、Action配置中的各項預設值

<span style="font-size:18px;"><package name="itcast" namespace="/test" extends="struts-default">        <action name="helloworld" class="cn.itcast.action.HelloWorldAction" method="execute" ><result name="success">/WEB-INF/page/hello.jsp</result>        </action>  </package> 1>如果沒有為action指定class,預設是ActionSupport。2>如果沒有為action指定method,預設執行action中的execute() 方法。3>如果沒有指定result的name屬性,預設值為success。</span>

二、Action中result的各種轉寄類型
<span style="font-size:18px;"><action name="helloworld" class="cn.itcast.action.HelloWorldAction"><result name="success">/WEB-INF/page/hello.jsp</result></action>

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

(2)在result中還可以使用${屬性名稱}運算式訪問action中的屬性,運算式裡的屬性名稱對應action中的屬性。如下:
<result type="redirect">/view.jsp?id=${id}</result>

(3)下面是redirectAction 結果類型的例子,如果重新導向的action中同一個包下: 
<action name="redirectAction"><result type="redirectAction">helloworld</result></action>

如果重新導向的action在別的命名空間下:
<struts>    <package name="itcast" namespace="/control/employee" extends="base"><action name="list" class="cn.itcast.action.HelloWorldAction" method="execute"><result name="success" type="redirect">/employeeAdd.jsp?username=${username}</result></action><action name="redirect"><result type="redirect">/employeeAdd.jsp</result></action><action name="redirectAction"> <!-- 不同的命名空間下的 --><result type="redirectAction"><param name="actionName">xxx</param> <!-- action的名字--><param name="namespace">/control/department</param></result></action></package><package name="other" namespace="/control/department" extends="base"><action name="xxx"><result>/WEB-INF/page/hello.jsp</result></action></package></struts>


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






struts2設定檔的result,跳轉到其它命名空間裡的action,怎寫

需要通過param進行設定
<result name="suc" type="redirectAction">
<param name="namespace">/Tree</param>
<param name="actionName">treeLoadAction</param>
</result>
 
對於struts2在strutsxml中預設action配置的問題

當然啦.路徑的地址就是這樣的:localhost:8080/項目名/namespace的值/action的name值.
如果沒有namespace這個屬性就成了localhost:8080/項目名//action的name值.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.