Result Type(Struts.xml配置)

一個提交到服務器的處理一般能夠分爲兩個階段:
第一個階段查詢服務器狀態(查詢或者更新數據庫)
第二個階段選擇一個合適的結果頁面其返回給用戶(這裏要講的Result的內容)。
java

Struts2提供了對不一樣種類返回結果的支持,常見的有JSP,FreeMarker,Velocity等。
Struts2支持的不一樣類型的返回結果爲:
名字
說明
Chain Result
用來處理Action鏈
Dispatcher Result
用來轉向頁面,一般處理JSP
FreeMarker Result
處理FreeMarker模板
HttpHeader Result
用來控制特殊的Http行爲
Redirect Result
重定向到一個URL
Redirect Action Result
重定向到一個Action
Stream Result
向瀏覽器發送InputSream對象,一般用來處理文件下載
Velocity Result
處理Velocity模板
XLS Result
處理XML/XLST模板
PlainText Result
顯示原始文件內容,例如文件源代碼
S2PLUGINS:Tiles Result
結合Tile使用
另外第三方的Result類型還包括JasperReports Plugin,專門用來處理JasperReport類型的報表輸出。
在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" />
<!-- Deprecated name form scheduled for removal in Struts 2.1.0.
The camelCase versions are preferred. See ww-1707 -->
<result-type name="redirect-action"
class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="plaintext"
class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
從上述代碼中能夠看出在不指定Result類型的時候使用dispatcher類型。
定義一個Result值,
<result name="success" type="dispatcher">
<param name="location">/ThankYou.jsp</param>
</result>
因爲type默認值是dispatcher,因此這裏不須要定義,另外name的默認值爲success因此這裏也不須要定義。
上述代碼能夠簡寫爲:
<result>
<param name="location">/ThankYou.jsp</param>
</result>
另外location參數也能夠直接卸載result標籤內部,因此上述代碼的最簡單的寫法爲:
<result>/ThankYou.jsp</result>
咱們也能夠定義多個不一樣的Result
<action name="Hello">
<result>/hello/Result.jsp</result>
<result name="error">/hello/Error.jsp</result>
<result name="input">/hello/Input.jsp</result>
</action>
上述代碼的含義爲,名字爲Hello的Action有三個返回結果,而且都是dispatcher類型(默認類型),這三個返回值的名字分別爲success(默認值),error,input,對應的頁面的路徑分別爲/hello/Result.jsp,/hello/Error.jsp,/hello/Input.jsp。
有些時候咱們須要一個定義在全局的Result,這個時候咱們能夠在package內部定義全局的Result,例如:
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirect-action">Logon!input</result>
</global-results>
動態返回結果
有些時候,只有當Action執行完璧的時候咱們才知道要返回哪一個結果,這個時候咱們能夠在Action內部定義一個屬性,這個屬性用來存儲Action執行完璧以後的Result值,例如:
private String nextAction;
public String getNextAction() {
return nextAction;
}
在strutx.xml配置文件中,咱們能夠使用${nextAction}來引用到Action中的屬性,經過${nextAction}表示的內容來動態的返回結果,例如:
<action name="fragment" class="FragmentAction">
<result name="next" type="redirect-action">${nextAction}</result>
</action>
上述Action的execute方法返回next的時候,還須要根據nextAction的屬性來判斷具體定位到哪一個Action。
相關文章
相關標籤/搜索