waysun一路陽(yáng)光

          不輕易服輸,不輕言放棄.--心是夢(mèng)的舞臺(tái),心有多大,舞臺(tái)有多大。踏踏實(shí)實(shí)做事,認(rèn)認(rèn)真真做人。

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 64 評(píng)論 :: 0 Trackbacks
          http://www.aygfsteel.com/19851985lili/articles/97664.html
          你的代碼本身有問(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

          actioncjsp傳值的問(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>

          posted on 2008-06-20 08:37 weesun一米陽(yáng)光 閱讀(5612) 評(píng)論(1)  編輯  收藏 所屬分類: 總結(jié)備用

          評(píng)論

          # re: action向jsp傳值問(wèn)題[轉(zhuǎn)總結(jié)] [未登錄](méi) 2013-02-05 16:40 123
          123  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 襄垣县| 禹州市| 营口市| 商都县| 丹江口市| 北票市| 遂宁市| 天峻县| 女性| 贵州省| 通渭县| 堆龙德庆县| 兰溪市| 金平| 昆明市| 寿光市| 万盛区| 岑巩县| 顺平县| 仁化县| 乌什县| 那曲县| 同江市| 宿迁市| 马关县| 菏泽市| 灵武市| 玉龙| 甘孜| 阳江市| 天峨县| 故城县| 牡丹江市| 武汉市| 梁河县| 灵石县| 科技| 赞皇县| 云南省| 隆安县| 塔河县|