<div id="cnblogs_post_body" class="blogpost-body"><p>建立一個實體:裏面包含須要接受的多種類型的參數。如實體、list集合、單個參數。(由於springMVC沒法同時接受這幾種參數,須要建立一個實體包含它們來接受)</p> <p>如接收User(bean裏面包含List)集合參數:</p> <p>一、User實體類:</p> <div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="複製代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="複製代碼"></a></span></div> <pre>1.<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> User { </span>2. <span style="color: #0000ff;">private</span><span style="color: #000000;"> String name; </span>3. <span style="color: #0000ff;">private</span><span style="color: #000000;"> String pwd; </span>4. <span style="color: #0000ff;">private</span> List<User> customers;<span style="color: #008000;">//</span><span style="color: #008000;">屬於用戶的客戶羣 </span> 5. <span style="color: #008000;">//</span><span style="color: #008000;">省略getter/setter </span> 6.} </pre> <div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="複製代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="複製代碼"></a></span></div></div> <p>二、頁面js代碼:</p> <div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="複製代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="複製代碼"></a></span></div> <pre>1.<span style="color: #0000ff;">var</span> customerArray = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Array(); </span>2.customerArray.push({name: "李四",pwd: "123"<span style="color: #000000;">}); </span>3.customerArray.push({name: "張三",pwd: "332"<span style="color: #000000;">}); </span>4.<span style="color: #0000ff;">var</span> user =<span style="color: #000000;"> {}; </span>5.user.name = "李剛"<span style="color: #000000;">; </span>6.user.pwd = "888"<span style="color: #000000;">; </span>7.user. customers =<span style="color: #000000;"> customerArray; </span>8<span style="color: #000000;">.$.ajax({ </span>9. type: "POST"<span style="color: #000000;">, </span>10. url: "<%=path%>/catalog.do?fn=saveUsers"<span style="color: #000000;">, </span>11. data: JSON.stringify(user),<span style="color: #008000;">//</span><span style="color: #008000;">將對象序列化成JSON字符串 </span> 12. dataType:"json"<span style="color: #000000;">, </span>13. contentType : 'application/json;charset=utf-8', <span style="color: #008000;">//</span><span style="color: #008000;">設置請求頭信息 </span> 14. success: <span style="color: #0000ff;">function</span><span style="color: #000000;">(data){ </span>15<span style="color: #000000;">. … </span>16<span style="color: #000000;">. }, </span>17. error: <span style="color: #0000ff;">function</span><span style="color: #000000;">(res){ </span>18<span style="color: #000000;">. … </span>19<span style="color: #000000;">. } </span>20.}); </pre> <div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="複製代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="複製代碼"></a></span></div></div> <p>三、Controller方法:</p> <div class="cnblogs_code"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="複製代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="複製代碼"></a></span></div> <pre>1<span style="color: #000000;">.@Controller </span>2.@RequestMapping("/catalog.do"<span style="color: #000000;">) </span>3.<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> CatalogController { </span>4<span style="color: #000000;">. </span>5. @RequestMapping(params = "fn=saveUsers"<span style="color: #000000;">) </span>6<span style="color: #000000;">. @ResponseBody </span>7. <span style="color: #0000ff;">public</span><span style="color: #000000;"> AjaxJson saveUsers(@RequestBody User user) { </span>8. List<User> customers =<span style="color: #000000;"> user.getCustomers(); </span>9<span style="color: #000000;">. … </span>10<span style="color: #000000;">. } </span>11.} </pre> <div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a href="javascript:void(0);" onclick="copyCnblogsCode(this)" title="複製代碼"><img src="//common.cnblogs.com/images/copycode.gif" alt="複製代碼"></a></span></div></div> <p> </p></div>javascript