struts2的action之间和action向jsp的参数传递
1,在 Action中我們經(jīng)常會從這個Action跳轉(zhuǎn)到另一個Action中,這時我們就要用到 redirect、redirect-action、chain,
? ? ? 其中redirect和redirect-action是重定向,相當(dāng)于瀏覽器二次請求,之前的數(shù)據(jù)都會丟失,所有的參數(shù)、值棧和請求屬性都會丟失。chain是服務(wù)器轉(zhuǎn)發(fā)action,源Action的用戶請求參數(shù)、值棧、session的數(shù)據(jù)賦值給目的Action,即在目的action中可以獲得源action得到的請求參數(shù)值棧,session等數(shù)據(jù)。
?當(dāng)我們想在這兩個action之間傳遞參數(shù)時: ??
? a,使用鏈接加“?”的方式。如果想在請求的過程中添加參數(shù)可以像超鏈接傳值一樣在最后用"xx?xx=xx" 的形式來傳遞參數(shù).
如:
<result name="reload" type="redirectAction">/manager/ managerInfo/account.action?參數(shù)名=參數(shù)值</result>,如果這里的參數(shù)值不是一個固定值可以用"${參 數(shù)值}"這種OGNL表達(dá)式的形式獲取,注意:這里的"參數(shù)值"在相應(yīng)的Action中一定要提供get/set方法,否則將無法獲得參數(shù)值,當(dāng)然這里也同樣可以取一個 對象的某個屬性值即:${對象名.屬性名},符合ONGL表達(dá)式訪問規(guī)則,之前寫過著方面的文章。
? ?b,使用子元素的形式。
<span style="background-color: rgb(255, 255, 255);"><action name="action1" method="" class=""><result name="success" type="chain"><param name="actionName">action2</param><param name="param1">${param1}</param><param name="param2">${param2}</param></result> </action> </span>
注意:
1. 若param1和param2是兩個變量,那么在兩個action中都要有他們的getter和setter
2. 若param1是常量,那么在action1中只要有一個param1的getter,在action2中要有param1的getter和setter
如果在兩個action之間直接跳轉(zhuǎn)而不用傳參數(shù),struts.xml文件可以和上面一樣,只是沒有param標(biāo)簽,還可以使用如下配置:
<action name="action1" method="" class=""><result name="success" type="redirectAction">action2.action</result> </action>
即可
2,struts2 action 向jsp頁面?zhèn)鬟f參數(shù)問題
? ?a,在jsp頁面可以使用ONGL表達(dá)式訪問action中的值棧等
? ?b,可以使用request.setAttribute("key","value");session.setAttribute("key","value");在相應(yīng)的jsp頁面上可以使用OGNL表達(dá)式“#request.key”“#session.key”獲取值。
總結(jié)
以上是生活随笔為你收集整理的struts2的action之间和action向jsp的参数传递的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: struts2中result的type跳
- 下一篇: struts2 的form 标签them