请教前辈如何从JSP页面往SpringMVC的Controller内传集合类型对象

如题所述

controller 方法中用

@RequestBody List<Bean> list 参数接收机和类型数据

前台如果是ajax提交

使用   JSON.stringify(list)   格式化下数组

请求头设置  contentType : 'application/json;charset=utf-8' 

例子

 var customerArray = new Array();
            customerArray.push({id: "1", name: "李四", pwd: "123"});
            customerArray.push({id: "2", name: "张三", pwd: "332"});
            $.ajax({
                url: "xx",
                type: "POST",
                contentType : 'application/json;charset=utf-8', //设置请求头信息
                dataType:"json",
                data: JSON.stringify(customerArray),
                success: function(data){
                    
                },
                error: function(res){
                    alert(res.responseText);
                }
            }); 
 
 @RequestMapping(value = "/xx", method ={RequestMethod.POST})
    @ResponseBody
    public String method1(@RequestBody List<User> users)
            throws Exception{
        System.out.println(users);
        return null;
    }

温馨提示:内容为网友见解,仅供参考
第1个回答  2018-06-28
页面的属性可定是对象获取的,你在Controller层用list接收前台穿过来的只直接对象点属性就能获取本回答被网友采纳
第2个回答  2017-09-22
在页面拼接json字符串,在Controller用JSONArray解析
相似回答