第八章 Result Types
一個(gè)提交到服務(wù)器的處理通常可以分為兩個(gè)階段,第一個(gè)階段查詢服務(wù)器狀態(tài)(查詢或者更新數(shù)據(jù)庫(kù)),第二個(gè)階段選擇一個(gè)合適的結(jié)果頁(yè)面其返回給用戶(這里要講的Result的內(nèi)容)。
 
Struts2提供了對(duì)不同種類返回結(jié)果的支持,常見(jiàn)的有JSP,F(xiàn)reeMarker,Velocity等。
Struts2支持的不同類型的返回結(jié)果為:
名字
說(shuō)明
Chain Result
用來(lái)處理Action鏈
Dispatcher Result
用來(lái)轉(zhuǎn)向頁(yè)面,通常處理JSP
FreeMarker Result
處理FreeMarker模板
HttpHeader Result
用來(lái)控制特殊的Http行為
Redirect Result
重定向到一個(gè)URL
Redirect Action Result
重定向到一個(gè)Action
Stream Result
向?yàn)g覽器發(fā)送InputSream對(duì)象,通常用來(lái)處理文件下載
Velocity Result
處理Velocity模板
XLS Result
處理XML/XLST模板
PlainText Result
顯示原始文件內(nèi)容,例如文件源代碼
S2PLUGINS:Tiles Result
結(jié)合Tile使用
另外第三方的Result類型還包括JasperReports Plugin,專門用來(lái)處理JasperReport類型的報(bào)表輸出。
 
在struts-default.xml文件中已經(jīng)有了對(duì)于所有類型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類型的時(shí)候使用dispatcher類型。
 
定義一個(gè)Result值,
<result name="success" type="dispatcher">
    <param name="location">/ThankYou.jsp</param>
</result>
由于type默認(rèn)值是dispatcher,所以這里不需要定義,另外name的默認(rèn)值為success所以這里也不需要定義。
上述代碼可以簡(jiǎn)寫為:
<result>
    <param name="location">/ThankYou.jsp</param>
</result>
另外location參數(shù)也可以直接卸載result標(biāo)簽內(nèi)部,所以上述代碼的最簡(jiǎn)單的寫法為:
<result>/ThankYou.jsp</result>
 
我們也可以定義多個(gè)不同的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有三個(gè)返回結(jié)果,并且都是dispatcher類型(默認(rèn)類型),這三個(gè)返回值的名字分別為 success(默認(rèn)值),error,input,對(duì)應(yīng)的頁(yè)面的路徑分別為/hello/Result.jsp,/hello/Error.jsp, /hello/Input.jsp。
 
有些時(shí)候我們需要一個(gè)定義在全局的Result,這個(gè)時(shí)候我們可以在package內(nèi)部定義全局的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>
 
動(dòng)態(tài)返回結(jié)果
有些時(shí)候,只有當(dāng)Action執(zhí)行完璧的時(shí)候我們才知道要返回哪個(gè)結(jié)果,這個(gè)時(shí)候我們可以在Action內(nèi)部定義一個(gè)屬性,這個(gè)屬性用來(lái)存儲(chǔ)Action執(zhí)行完璧之后的Result值,例如:
private String nextAction;
 
public String getNextAction() {
    return nextAction;
}
 
在strutx.xml配置文件中,我們可以使用${nextAction}來(lái)引用到Action中的屬性,通過(guò)${nextAction}表示的內(nèi)容來(lái)動(dòng)態(tài)的返回結(jié)果,例如:
<action name="fragment" class="FragmentAction">
 <result name="next" type="redirect-action">${nextAction}</result>
</action>
 
上述Action的execute方法返回next的時(shí)候,還需要根據(jù)nextAction的屬性來(lái)判斷具體定位到哪個(gè)Action。


ExtJS教程- Hibernate教程-Struts2 教程-Lucene教程