首先有一個假設,這兩個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"這個,就可以了