<div class="content" id="articleContent"> <div class="ad-wrap"> <p style="margin:0 0 10px 0;"><a data-traceid="blog_detail_above_text_link_1" data-tracepid="blog_detail_above_text_link" style="color:#A00;font-weight:bold;" href="http://clickc.admaster.com.cn/c/a109734,b2619652,c3159,i0,m101,8a1,8b2,h" target="_blank">華爲雲4核8G,高性能雲服務器,免費試用 >>> </a> <img src="https://www.oschina.net/img/hot3.png" align="absmiddle" style="max-height: 32px; max-width: 32px;"></p> </div> <p>SpringMVC 多個對象的相同字段參數傳遞解決方案,在SpringMVC中,有時須要傳遞多個對象(除了Model和web元素)</p> <p>衆所周知,在SpringMVC中不支持相似struts2的點語法傳參法則,所以多個對象同名參數傳遞勢必會形成必定的問題。</p> <p><br></p> <ol> <li><p>SpringMVC的參數傳遞很是智能,可自動裝配參數到對象</p></li> </ol> <pre><code><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">User</span>{ <span class="hljs-keyword">private</span> String name; <span class="hljs-keyword">private</span> String id; <span class="hljs-keyword">private</span> String gender, <span class="hljs-keyword">private</span> String age; <span class="hljs-comment">//getter 和setter</span> } ---------------------------------- <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Food</span>{ <span class="hljs-keyword">private</span> String name; <span class="hljs-keyword">private</span> String id; <span class="hljs-keyword">private</span> String price, <span class="hljs-keyword">private</span> String date; <span class="hljs-comment">//getter 和setter</span> } ------- <form method=<span class="hljs-string">'post'</span> action=<span class="hljs-string">'url'</span>> 用戶名 <input type=<span class="hljs-string">'text'</span> name=<span class="hljs-string">'name'</span>> 用戶id <input type=<span class="hljs-string">'text'</span> name=<span class="hljs-string">'id'</span>> 食品名 <input type=<span class="hljs-string">'text'</span> name=<span class="hljs-string">'name'</span>> 食品id <input type=<span class="hljs-string">'text'</span> name=<span class="hljs-string">'id'</span>> <input type=<span class="hljs-string">'text'</span> name=<span class="hljs-string">'gender'</span>> <input type=<span class="hljs-string">'text'</span> name=<span class="hljs-string">'age'</span>> <input type=<span class="hljs-string">'text'</span> name=<span class="hljs-string">'price'</span>> <input type=<span class="hljs-string">'text'</span> name=<span class="hljs-string">'date'</span>> </form>javascript
@requestMap(<span class="hljs-keyword">value</span>={<span class="hljs-string">'/order/book'</span>}) <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> <span class="hljs-title">show</span>(<span class="hljs-params">User u,Food f</span>)</span>{}</code></pre>java
<p>在上述狀況下User 和food都不能獲得正確的name和id,或者說更本得不到,這要是struts2就方便多了,直接使用點語法 u.name,u.id,f.name,f.id....</p> <p><br></p> <p>爲了解決上述問題,有2中方案:</p> <p><br></p> <span id="OSC_h4_1"></span> <h4 id="h4_1"><strong>1.是創建一個DTO</strong></h4> <p>優勢是能夠認爲的避免參數的傳遞,直接改dto的字段名 u_name,u_id,f_name,f_id,gender,age,date,price都要列出來,而後做以下操做</p> <p>而後修改表單字段和contoller</p> <pre><code class="hljs xml"><span class="hljs-tag"><<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">'post'</span> <span class="hljs-attr">action</span>=<span class="hljs-string">'url'</span>></span> 用戶名 <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'u_name'</span>></span> 用戶id <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'u_id'</span>></span> 食品名 <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'f_name'</span>></span> 食品id <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'f_id'</span>></span> <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'gender'</span>></span> <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'age'</span>></span> <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'price'</span>></span> <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'date'</span>></span> <span class="hljs-tag"></<span class="hljs-name">form</span>></span>web
@requestMap(value={'/order/book'}) public string show(UserFoodDto dto){ //而後拆分,將字段 User u = new User(); Food f = new Food();spring
u.setName(dto.getUname()); f.setName(dto.getname());segmentfault
u.setUid(dto.getUid()); f.setFid(dto.getFid());服務器
u.setAge(dto.getAge); f.setPrice(dto.getPrice); ..... }</code></pre>mvc
<p>缺點是:若是數據量大,100百個字段,修改的地方天然越多,並且一個dto,拆分也很費力,所以不建議使用在數據量大的狀況下</p> <p><br></p> <span id="OSC_h4_2"></span> <h4 id="h4_2"><span style="color: rgb(89, 89, 89);"><strong>2.<span style="color: rgb(0, 176, 80);">使用橋鏈接,將不一樣的部分專門封裝成一個對象</span></strong></span><strong>(<span style="color: rgb(255, 0, 0);">推薦</span>)</strong></h4> <p>優勢:減小了衝突,減小了工做量</p> <p>缺點:衝突字段/總字段趨近於1時顯得不必(不過,這種狀況幾乎不多出現,除非是繼承關係,但繼承關係徹底可使用子類來傳值,因此這個問題你就當沒遇到過)</p> <p><br></p> <p>表單以下</p> <pre ><code class="hljs xml"><span class="hljs-tag"><<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">'post'</span> <span class="hljs-attr">action</span>=<span class="hljs-string">'url'</span>></span> 用戶名 <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'u_name'</span>></span> 用戶id <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'u_id'</span>></span> 食品名 <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'f_name'</span>></span> 食品id <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'f_id'</span>></span> <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'gender'</span>></span> <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'age'</span>></span> <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'price'</span>></span> <span class="hljs-tag"><<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">'text'</span> <span class="hljs-attr">name</span>=<span class="hljs-string">'date'</span>></span> <span class="hljs-tag"></<span class="hljs-name">form</span>></span>app
@requestMap(value={'/order/book'}) public string show(User u,Food f,UFBridge ufb){async
u.setName(ufb.getUname()); f.setName(ufb.getUname()); u.setId(ufb.getUid); f.setId(ufb.getFid); }</code></pre>post
<p><br></p> <span id="OSC_h4_3"></span> <h4 id="h4_3">3.使用相應的註解@InitBinder,能夠作出相似struts2的方案(不推薦,代碼量增長,靈活度不夠)</h4> <p>以下博文</p> <p><a href="http://segmentfault.com/a/1190000002923372" target="_blank" rel="nofollow">SpringMVC同名參數綁定問題</a><br></p> <p><a href="http://www.tuicool.com/articles/rE3YJn" target="_blank" rel="nofollow">springmvc註解形式的開發參數接收</a><br></p> <p><br></p> <p>try doing it</p> <div class="ad-wrap"> <div data-traceid="blog_down_1" data-tracepid="blog_down" style="text-align:center"> <!-- oschina-blog-728x90 --> <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-7090564139599510" data-ad-slot="5590362768"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <script type="text/javascript"> function googleAdJSAtOnload() { var element = document.createElement("script"); element.src = "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"; element.async = true; document.body.appendChild(element); } if (window.addEventListener) { window.addEventListener("load", googleAdJSAtOnload, false); } else if (window.attachEvent) { window.attachEvent("onload", googleAdJSAtOnload); } else { window.onload = googleAdJSAtOnload; } </script> </div> </div> </div>