springMVC傳遞一組對象的接受方式


受益此大神:https://blog.csdn.net/cgd_8523/article/details/80022331

同時借鑒代碼!!!!

我只用了一種方法,就記下這一種

需求:前台存在動態添加對象(點擊添加,增加一行,每行都是對象的屬性)

准備后台:

1、建立公用模型,用來接收前台傳遞的集合數據。注意list的泛型,這里需給出要傳遞對象,我用過泛型T,白搭。。。

ublic class UserModel {
    private List<User> users;
 
    public List<User> getUsers() {
        return users;
    }
 
    public void setUsers(List<User> users) {
        this.users = users;
    }
 
    public UserModel(List<User> users) {
        super();
        this.users = users;
    }
 
    public UserModel() {
        super();
    }
 
}

2、編寫Controller

@RequestMapping(value = "/submitUserList_2", method ={RequestMethod.POST})
    @ResponseBody
    public String submitUserList_2(UserModel users)
            throws Exception{
        String result = "";
        List<User> userList = users.getUsers();
        if(userList == null || userList.size() <= 0){ return "No any ID.中文"; }
        result = this.showUserList(userList);
        return result;
    }

3、前台控制,form表單,簡單點兒寫,這個不重要

  <form action="/user/submitUserList_2" method="post">
    <table>
<tr>
       <td> ID:<input type="text" name="users[0].id"></td>
        <td>Username:<input type="text" name="users[0].name"></td>
       <td> Password:<input type="text" name="users[0].pwd"></td>
         <input type="submit" value="Submit">
</tr>
</table>
    </form>

 

4、js因為我這是靈活的form,所以給出一個比較變態的方法

var f=document.forms[0];
              $(f).find("tr").each(function(i){
                  i=i-4;
                  if(i>=0)
                  $(this).find("input").each(function(){
                      var n=$(this).attr("name");
                      var _n="users["+i+"]."+n;
                      $(this).attr("name",_n);
                  });
              });

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM