http://www.tkk7.com/19851985lili/articles/97664.html
你的代碼本身有問題,一般來說,我們在使用Struts時,如果要在JSP隱式的傳值給Action有兩種情況:
1、要傳的值是FromBean中的一個字段,你說的情況應該就是這種情況,例如需要在Edit頁面中保存theID,在Action中執行Update操作時根據ID來更新數據庫的值,你可以這樣做:
Jsp中的代碼為:<html:hidden property="theID" />
提交后,theID的值就會放到FormBean中的theID中,你就可以通過getTheID()來獲得這個值。
2、要傳的值不是FromBean中的一個字段:
Jsp中的代碼為:
<input type="hidden" name="XXX" value="<%=request.getAttribute(XXX)%>">
當然,你應該在Action中就已經就這個值放到了request中,request.setAttribute("XXX",value);,
然后在Action中你才可以通過request.getParameter("XXX");來取得這個值。
補充一點,request.setAttribute("XXX",value);中,value應該是個String,還有,<input type="hidden" name="XXX" value="<%=request.getAttribute(XXX)%>">應該改為
<input type="hidden" name="XXX" value="<%=(String)request.getAttribute(XXX)%>">
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
http://blog.chinaunix.net/u1/55983/showart_522992.html
到actionc向jsp傳值的問題,開始以為自己編寫程序有問題,檢查了幾天都沒解決,網上的解決方案也不可行。直到今天在網上找到一個可行的解決方案,現在總結如下:
問題:
在Action中使用request.setAttribute("key", Value)的方式設置屬性,在通過return mapping.findForward("Forwards")方式跳轉,但在對應的JSP頁面中無得取得傳過來的屬性值。
原因:
由于request生命周期只在一次請求范圍內有效的,所以如果使用了struts-action 中的Reditect設置的話,就會將請求重定向,也就是破壞了request生命周期,重新產生一次請求,那么在jsp頁面中,request.setAttribute設置過的屬性被清空了。
解決:
在新建Action時,在選擇Forwards時,不要選擇“Redirect”,或者在struts-config.xml配置文件中,將對應<Action>標簽中的<Forward>標簽中,設置“Redirect”值為false即可。
測試:下面是我項目中的舉例
1、在ListMarket.java中部分代碼如下:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
// 列出所有Market的信息
MarketService ms = new MarketService();
List listMarket = (List) ms.findAllMarket(); //從業務層取得LIST
request.setAttribute("listMarket", listMarket);
return mapping.findForward("success");
}
2、Struts-config.xml中的設置如下:
<action path="/listMarket"
type="com.sailor.struts.action.ListMarketAction" scope="request">
<forward name="success" path="/list.jsp" redirect="false" />
</action>
3、在jsp頁面實現:
<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>