在action的指定方法執行完畢後總會返回一個字符串,struts2根據返回的字符串去action的配置中的result去找匹配的名字,根據配置執行下一步的操作。
在ActionSupport基類中定義了五個標準的返回值
String SUCCESS = "success";
String NONE = "none";
String ERROR = "error";
String INPUT = "input";String LOGIN = "login";
當然我們可以自己隨意定義返回的名字
result元素有兩個用處,首先它提供一個邏輯上名字。一個action可以單純的返回success或input而不用理會之後的具體細節。第二,result元素提供type屬性,可以不僅僅的返回一個jsp頁面,而可以實現更多有意思的功能。
每個包可以有自己的默認result type,當一個result沒指定type時將使用默認。正常情況下默認的result type爲dispatcher。如果一個包繼承另一個包,這個包可以實現自己的默認result也可以繼承父包的默認result。手動指定一個默認的result如下
<result-types>
<result-type name="dispatcher" default="true"
class="org.apache.struts2.dispatcher.ServletDispatcherResult" />
</result-types>
同時,一個result的name屬性也有默認值success。最平常的情況下,一個result如下
<result>
/ThankYou.jsp
</result>
一個action可以有多個不同的result
<action name="Hello">
<result>/hello/Result.jsp</result>
<result name="error">/hello/Error.jsp</result>
<result name="input">/hello/Input.jsp</result>
</action>
全局result
有些時候,一些result是可以爲所有的action服務的,例如出錯頁面的result,登陸頁面的result
我們可以定義一些全局result供同一個包的所有actoin共享。注意首先struts2會先搜索局部result,如果沒找到則會去全局result尋找匹配的result
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirectAction">Logon!input</result>
</global-results>
result 的動態參數配置
有些時候我們需要從一個action轉向另一個action,但是參數卻是運行才能知道,可以用一下的方法實現。下面用一個例子來說明
<struts>
....
<package name="somePackage" namespace="/myNamespace" extends="struts-default">
<action name="myAction" class="com.project.MyAction">
<result name="success" type="redirectAction">otherAction?id=${id}</result>
<result name="back" type="redirect">${redirectURL}</result>
</action>
<action name="otherAction" class="com.project.MyOtherAction">
...
</action>
</package>
....
</struts>
在action中必須有id,redirectURL屬性以及它們的get方法
public class MyAction extends ActionSupport {
private int id;
private String redirectURL;
...
public String execute() {
...
if (someCondition) {
this.redirectURL = "/the/target/page.action";
return "back";
}
this.id = 123;
return SUCCESS;
}
public int getId() { return this.id; }
public void setId(int id) { this.id = id; }
public String getRedirectURL() { return this.redirectURL; }
public void setRedirectURL(String redirectURL){this.redirectURL=redirectURL;}
...
}
如果返回success的話將會轉到/<app-prefix>/myNamespace/otherAction.action?id=123
當一個result有多個參數時可以通過param子屬性指定,在後面會有例子
result 的types
result有許多不同的types,用來實現不同的功能,struts2默認的types有如下幾個
Dispatcher 轉到一個視圖頁面,通常爲jsp頁面。這個是默認的type值。
<result>
/ThankYou.jsp
</result>
Stream 將原始數據字節發送給瀏覽器,通常用於下載文件
contentType 發送給瀏覽器的流的mime-type (默認text/plain).
contentLength- 流的長度 (便於瀏覽器顯示下載進度).
contentDispostion- 設置響應頭contentDispostion的值(默認inline)
這個我不太清楚是什麼意思,Google了一下也沒什麼好的解釋
InputName action提供的輸入流的屬性名稱(默認inputStream).
bufferSize從輸入流寫入到輸出流的緩存大小(默認1024字節).
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">imageStream</param>
<param name="bufferSize">1024</param>
</result>
PlainText 一般用來顯示一個jsp或html頁面的原始內容
<action name="displayJspRawContent" >
<result type="plaintext">/myJspFile.jsp</result>
</action>
redirectAction 定向到另一個action 覺得沒什麼太大的用處,舉個例子留作參考吧
<package name="public" extends="struts-default">
<action name="login" class="...">
<!-- Redirect to another namespace -->
<result type="redirect-action">
<param name="actionName">dashboard</param>
<param name="namespace">/secure</param>
</result>
</action>
</package>
<package name="secure" extends="struts-default" namespace="/secure">
<-- Redirect to an action in the same namespace -->
<action name="dashboard" class="...">
<result>dashboard.jsp</result>
<result name="error" type="redirect-action">error</result>
</action>
<action name="error" class="...">
<result>error.jsp</result>
</action>
</package>
<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
<-- Pass parameters (reportType, width and height) -->
<!--
The redirect-action url generated will be :
/genReport/generateReport.action?reportType=piewidth=100height=100
-->
<action name="gatherReportInfo" class="...">
<result name="showReportResult" type="redirect-action">
<param name="actionName">generateReport</param>
<param name="namespace">/genReport</param>
<param name="reportType">pie</param>
<param name="width">100</param>
<param name="height">100</param>
</result>
</action>
</package>
感覺type裏就這幾個用處多一些,其他的幾個就不寫了。以後有用的時候另外再寫