你的代碼本身有問(wèn)題,一般來(lái)說(shuō),我們?cè)谑褂肧truts時(shí),如果要在JSP隱式的傳值給Action有兩種情況:
1、要傳的值是FromBean中的一個(gè)字段,你說(shuō)的情況應(yīng)該就是這種情況,例如需要在Edit頁(yè)面中保存theID,在Action中執(zhí)行Update操作時(shí)根據(jù)ID來(lái)更新數(shù)據(jù)庫(kù)的值,你可以這樣做:
Jsp中的代碼為:<html:hidden property="theID" />
提交后,theID的值就會(huì)放到FormBean中的theID中,你就可以通過(guò)getTheID()來(lái)獲得這個(gè)值。
2、要傳的值不是FromBean中的一個(gè)字段:
Jsp中的代碼為:
<input type="hidden" name="XXX" value="<%=request.getAttribute(XXX)%>">
當(dāng)然,你應(yīng)該在Action中就已經(jīng)就這個(gè)值放到了request中,request.setAttribute("XXX",value);,
然后在Action中你才可以通過(guò)request.getParameter("XXX");來(lái)取得這個(gè)值。
補(bǔ)充一點(diǎn),request.setAttribute("XXX",value);中,value應(yīng)該是個(gè)String,還有,<input type="hidden" name="XXX" value="<%=request.getAttribute(XXX)%>">應(yīng)該改為
<input type="hidden" name="XXX" value="<%=(String)request.getAttribute(XXX)%>">
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
http://blog.chinaunix.net/u1/55983/showart_522992.html
到actionc向jsp傳值的問(wèn)題,開(kāi)始以為自己編寫程序有問(wèn)題,檢查了幾天都沒(méi)解決,網(wǎng)上的解決方案也不可行。直到今天在網(wǎng)上找到一個(gè)可行的解決方案,現(xiàn)在總結(jié)如下:
問(wèn)題:
在Action中使用request.setAttribute("key", Value)的方式設(shè)置屬性,在通過(guò)return mapping.findForward("Forwards")方式跳轉(zhuǎn),但在對(duì)應(yīng)的JSP頁(yè)面中無(wú)得取得傳過(guò)來(lái)的屬性值。
原因:
由于request生命周期只在一次請(qǐng)求范圍內(nèi)有效的,所以如果使用了struts-action 中的Reditect設(shè)置的話,就會(huì)將請(qǐng)求重定向,也就是破壞了request生命周期,重新產(chǎn)生一次請(qǐng)求,那么在jsp頁(yè)面中,request.setAttribute設(shè)置過(guò)的屬性被清空了。
解決:
在新建Action時(shí),在選擇Forwards時(shí),不要選擇“Redirect”,或者在struts-config.xml配置文件中,將對(duì)應(yīng)<Action>標(biāo)簽中的<Forward>標(biāo)簽中,設(shè)置“Redirect”值為false即可。
測(cè)試:下面是我項(xiàng)目中的舉例
1、在ListMarket.java中部分代碼如下:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
// 列出所有Market的信息
MarketService ms = new MarketService();
List listMarket = (List) ms.findAllMarket(); //從業(yè)務(wù)層取得LIST
request.setAttribute("listMarket", listMarket);
return mapping.findForward("success");
}
2、Struts-config.xml中的設(shè)置如下:
<action path="/listMarket"
type="com.sailor.struts.action.ListMarketAction" scope="request">
<forward name="success" path="/list.jsp" redirect="false" />
</action>
3、在jsp頁(yè)面實(shí)現(xiàn):
<logic:present name="listMarket">
<logic:iterate id="market" name="listMarket" scope="request">!
id: <bean:write name="market" property="id"/>
year: <bean:write name="market" property="year"/>
quarter: <bean:write name="market" property="quarter"/>
consumer: <bean:write name="market" property="consumer"/>
presale: <bean:write name="market" property="preSale"/><br>
</logic:iterate>
</logic:present>