struts2中常用Result类型的用法
生活随笔
收集整理的這篇文章主要介紹了
struts2中常用Result类型的用法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、dispatcher
(1)為缺省的result類型,一般情況下我們在struts.xml會這么寫:
<result name="success">/main.jsp</result>
以上寫法使用了兩個默認(rèn),其完整的寫法為:
# <result name="success" type="dispatcher"> ?
#????????? <param name="location">/maini.jsp</param> ?
# </result>
第一個默認(rèn):type="dispatcher";第二個默認(rèn):設(shè)置的為location參數(shù),location只能是頁面,不能是另一個action(可用type="chain"解決)。
(2)實(shí)現(xiàn)方式
從doExecute方法看出,有三個出口(finalLocation為要跳轉(zhuǎn)的地址):
pageContext.include(finalLocation);
dispatcher.forward(request, response); (dispatcher是根據(jù)finalLocation創(chuàng)建的)
dispatcher.include(request, response);
而我們知道,forward與include都是轉(zhuǎn)發(fā)到context內(nèi)部的資源。
二、redirect
(1)可以重定向到一個頁面,另一個action或一個網(wǎng)址。
# <result name="success" type="redirect">aaa.jsp</result> ?
# <result name="success" type="redirect">bbb.action</result> ?
# <result name="success" type="redirect">www.baidu.com</result> ?
(2)實(shí)現(xiàn)方式:
查看doExecute方法,只有一個出口:
response.sendRedirect(finalLocation);
sendRedirect是重定向,是重新產(chǎn)生一個HTTP請求到服務(wù)器,故重定向后其原來所在的action上下文就不可用了。
三、chain
(1)主要用于把相關(guān)的幾個action連接起來,共同完成一個功能。
# <action name="step1" class="test.Step1Action"> ?
#????????? <result name="success" type="chain">step2.action</result> ?
# </action> ?
#? ?
# <action name="step2" class="test.Step2Action"> ?
#????????? <result name="success">finish.jsp</result> ?
# </action>
(2)實(shí)現(xiàn)方式:
查看execute()方法,主要思想如下:
// 根據(jù)Action名稱finalActionName及要調(diào)用的方法finalMethodName來new一個代理對象proxy,并執(zhí)行之
# proxy = actionProxyFactory.createActionProxy(finalNamespace,? ?
#??????????????????? finalActionName, finalMethodName, extraContext);? ?
# proxy.execute();
(3)多個action間數(shù)據(jù)的傳遞
主要有兩種方式:
1。由于處于chain中的action屬于同一個http請求,共享一個ActionContext,故可以在上下文中獲取,在頁面上可以直接使用。手動獲取的方法如下:
# HttpServletRequest request = ServletActionContext.getRequest();? ?
# String s=(String)request.getAttribute("propName"); ?
2。實(shí)現(xiàn)ModelDriven接口
在Step1Action中,加入getModel:
# public Object getModel() {??????? ?
#????????? return message;? ?
# }
在Step2Action中,加入setModel:
# public void setModel(Object o){? ?
#????????? System.out.println("message is:"+o);? ?
# }
注意,setModel的調(diào)用先于execute()方法后于構(gòu)造方法。 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
(1)為缺省的result類型,一般情況下我們在struts.xml會這么寫:
<result name="success">/main.jsp</result>
以上寫法使用了兩個默認(rèn),其完整的寫法為:
# <result name="success" type="dispatcher"> ?
#????????? <param name="location">/maini.jsp</param> ?
# </result>
第一個默認(rèn):type="dispatcher";第二個默認(rèn):設(shè)置的為location參數(shù),location只能是頁面,不能是另一個action(可用type="chain"解決)。
(2)實(shí)現(xiàn)方式
從doExecute方法看出,有三個出口(finalLocation為要跳轉(zhuǎn)的地址):
pageContext.include(finalLocation);
dispatcher.forward(request, response); (dispatcher是根據(jù)finalLocation創(chuàng)建的)
dispatcher.include(request, response);
而我們知道,forward與include都是轉(zhuǎn)發(fā)到context內(nèi)部的資源。
二、redirect
(1)可以重定向到一個頁面,另一個action或一個網(wǎng)址。
# <result name="success" type="redirect">aaa.jsp</result> ?
# <result name="success" type="redirect">bbb.action</result> ?
# <result name="success" type="redirect">www.baidu.com</result> ?
(2)實(shí)現(xiàn)方式:
查看doExecute方法,只有一個出口:
response.sendRedirect(finalLocation);
sendRedirect是重定向,是重新產(chǎn)生一個HTTP請求到服務(wù)器,故重定向后其原來所在的action上下文就不可用了。
三、chain
(1)主要用于把相關(guān)的幾個action連接起來,共同完成一個功能。
# <action name="step1" class="test.Step1Action"> ?
#????????? <result name="success" type="chain">step2.action</result> ?
# </action> ?
#? ?
# <action name="step2" class="test.Step2Action"> ?
#????????? <result name="success">finish.jsp</result> ?
# </action>
(2)實(shí)現(xiàn)方式:
查看execute()方法,主要思想如下:
// 根據(jù)Action名稱finalActionName及要調(diào)用的方法finalMethodName來new一個代理對象proxy,并執(zhí)行之
# proxy = actionProxyFactory.createActionProxy(finalNamespace,? ?
#??????????????????? finalActionName, finalMethodName, extraContext);? ?
# proxy.execute();
(3)多個action間數(shù)據(jù)的傳遞
主要有兩種方式:
1。由于處于chain中的action屬于同一個http請求,共享一個ActionContext,故可以在上下文中獲取,在頁面上可以直接使用。手動獲取的方法如下:
# HttpServletRequest request = ServletActionContext.getRequest();? ?
# String s=(String)request.getAttribute("propName"); ?
2。實(shí)現(xiàn)ModelDriven接口
在Step1Action中,加入getModel:
# public Object getModel() {??????? ?
#????????? return message;? ?
# }
在Step2Action中,加入setModel:
# public void setModel(Object o){? ?
#????????? System.out.println("message is:"+o);? ?
# }
注意,setModel的調(diào)用先于execute()方法后于構(gòu)造方法。 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的struts2中常用Result类型的用法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Struts2 Result详解
- 下一篇: Struts2-result类型