當(dāng)Action類的方法處理請求后,會返回一個字符串(邏輯視圖名),框架根據(jù)這個結(jié)果碼選擇對應(yīng)的result,向用戶輸出,所以需要在struts.xml提供<result>元素定義結(jié)果頁面,這個結(jié)果頁面可以是局部結(jié)果頁面或者全局結(jié)果頁面。
1、 局部結(jié)果頁面和全局結(jié)果頁面
1)局部結(jié)果頁面
1 <action name="result" class="cn.sunny.action.ResultAction">2 <!-- 局部結(jié)果當(dāng)前Action使用 -->3 <result name="success">/result.jsp</result> 4 </action>
2)全局結(jié)果頁面
1 <global-results>2 <!-- 全局結(jié)果 當(dāng)前包中 所有Action都可以用-->3 <result name="success">/result.jsp</result>4 </global-results>
2、結(jié)果頁面跳轉(zhuǎn)類型
在struts-default.xml定義了一些<result>中的type屬性的結(jié)果頁面類型,默認(rèn)的type類型是dispatcher轉(zhuǎn)發(fā),type屬性類型:
1 <result-types> 2 <!--用來處理Action鏈--> 3 <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> 4 <!--用來轉(zhuǎn)向頁面,通常處理jsp--> 5 <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> 6 <!--處理 FreeMarker 模板--> 7 <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> 8 <!--控制特殊http行為的結(jié)果類型--> 9 <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>10 <!--重定向到一個URL-->11 <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>12 <!--重定向到一個 Action-->13 <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>14 <!--向?yàn)g覽器發(fā)送 InputSream 對象,通常用來處理文件下載,還可用于返回 Ajax 數(shù)據(jù)。-->15 <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>16 <!--處理 Velocity 模板-->17 <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>18 <!--處理 XML/XLST 模板-->19 <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>20 <!--顯示源文件內(nèi)容,如文件源碼-->21 <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />22 </result-types>
所以type屬性可以取的值有:chain、dispatcher、freemarker、httpheader、redirect、redirectAction、stream、velocity、xslt、plainText。
新聞熱點(diǎn)
疑難解答
圖片精選