利用struts進行前端頁面間傳值及hibernate異常:a different object with the same identifier value was already associated with the session的總結

來源:互聯網
上載者:User

標籤:相同   mit   前端   size   struts   java   9.png   總結   href   

2017-3-16

我使用SSH架構在做單表CRUD的更新操作時遇到了一個問題,就是頁面間該怎麼傳值?解決該需求時引發了一系列的bug,趁還記得好好總結一番。

前端頁面間傳值

情景:在我查出所以記錄後,點擊修改會連結到新的修改頁面。

問題:該新頁面沒有之前的實體資訊,該如何傳遞要修改的實體資訊給該頁面,例如id?

思路1:利用struts的action來傳值。

1 <form action="deleteSerCate.action" method="post">2   <input type="hidden" name="serviceCategory.id" value="<s:property value="#list.id"/>">3   <input type="submit" value="刪除">4   <a href="serviceCId.action?serviceCategory.id=<s:property value="#list.id"/>">修改</a>5 </form>

注意看我<a>裡的url,我建立了一個action,並在url裡傳遞了id值。接著在建立的action裡只做返回"success",如下:

1 // 傳id到更新頁面2 public String serviceCId(){3     return "success";4 }

然後在struts.xml的相應action裡返回到更新頁面:

1 <!-- 傳id到更新頁面 -->2 <action name="serviceCId" class="serCateAction" method="serviceCId">3     <result name="success">/updateSerCate.jsp</result>4 </action>

完成後點擊“修改”後跳轉到更新頁面,此時頁面已經可以獲得需更新實體的id號了。如下:

思路2:同樣在url裡傳值,在新頁面裡用js擷取值。該思路還沒有實施過,但是感覺可行。待檢驗。

總結:前端頁面間傳值,前後端傳值是常碰到的情境,這裡我使用了action來傳值是一種方法,應該還有更好的方法,以後若有新經驗有待再總結。

 

 

hibernate異常:a different object with the same identifier value was already associated with the session

在更新操作中進行session.update(object);時報了上面錯誤。

原因:在hibernate中同一session裡有兩個相同標識但是是不同實體的情況,即2個不同的對象關聯到了同一個標誌位。例如我的情況就是session中有兩個對象關聯到同一個id,在session進行更新操作時不知道要更新哪一個對象。

解決方案:在update前進行session.clear();操作。如下:

 

1 /* 2  * 不clear一下會報a different object with the 3  * same identifier value was already 4  * associated with the session5  */6  session.clear();7  session.update(serviceCategory);

有時在進行session.clear()操作後會報"Found two representations of same collection"異常,具體原因我也不懂,有待理解與解決。

解決該問題時這篇文章對我協助最大,留言中也得到很多收穫:http://www.blogjava.net/hrcdg/articles/157724.html

 

利用struts進行前端頁面間傳值及hibernate異常:a different object with the same identifier value was already associated with the session的總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.