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