struts中在兩個action之間進行傳遞資料的方法

來源:互聯網
上載者:User

首先有一個假設,這兩個action中的form都是同一個類型,第一個action稱為action1,第二個action稱為action2
在strutts中,若從action1跳轉到action2的話,則在action2中,struts會重新從reqeust(或session或context)中取出資料,重新設定到form中.所以,若想在兩個action之間傳遞資料,不可以在action1中設定form,而是通過request,可以直接覆蓋想傳遞的屬性parameter或者attribute, 在第二個action中,struts會自動取出資料,設定到form中,也可以使用別的名稱,在action2手動取出這個資料,在手動設定到form中.
那為什麼呢?其實這不是struts的設計失誤,而是實際情況沒辦法做到用form來傳遞資料,因為action2不清楚這是從別的action傳遞過來的,有可能是從client端傳過來的.還有,action2也不知道這個form是從什麼地方來的.所以action2要重新設定form的值.

【songsongsong84】:
兩種方法:

一把參數放在 request.setAttribuite裡
在下個action 裡頭用request.get  或者的該值

還有個就 把參數放 session 的屬性裡頭

如果不知道具體怎麼操作就說一聲,我幫你寫,不過要記得,給分我需要分去問別人問題

【qulin52】:
這個方法是得不到值的,我早就試過了
request.setAttribute("sysType", sysTypeId);
get 到的是null

【qulin52】:
request.getSession().setAttribute("sysType",sysTypeId);
放session 中也是得不到

【qulin52】:
我以前在網上看到過 有個解決方案 只是比較麻煩的!

【songsongsong84】:
那不可能,,action 都是servlet  難道在兩個servlet中都不能用session 傳值

【qulin52】:
問題是等不到 ~ 我直接給我數值都得不到

【songsongsong84】:
怎麼寫的?

【qulin52】:
request.getSession().setAttribute("sysType",sysTypeId);
request.getSession().setAttribute("netType",netTypeId);

sysTypeId=(String) request.getAttribute("sysType");
netTypeId=(String) request.getAttribute("netType");

【qulin52】:
我確定systypeId在傳的時候不為空白!

【songsongsong84】:
sysTypeId=(String) request.getAttribute("sysType");
netTypeId=(String) request.getAttribute("netType");

改為:

你要記得 HttpSession session=request.getSession();
sysTypeId=(String) session.getAttribute("sysType");
netTypeId=(String) session.getAttribute("netType");

【songsongsong84】:
你根本沒寫對啊,你變數放在session 的屬性裡頭就的用session 來擷取啊,不要用request 啊

【qulin52】:
我試試

【mianwo602】:
<action path="/tawwpyear/checklistpass" parameter="CHECKLISTPASS"
type="com.boco.eoms.workplan.controller.TawwpYearAction"
scope="request" validate="false">
<forward name="success" path="/tawwpyear/checklist.do"
redirect="true" />
</action>
你這裡寫了 redirect="true",這個是直接跳轉,也就是說,你在第一個action中set的值,在第2個action,中是擷取不到的。用session可以,我覺得不要怎麼做
解決辦法:去掉redirect="true"這個,就可以了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.