java后台接收前端对象数组

wylc123 1年前 ⋅ 557 阅读

思路:首先,各位肯定知道我们如何传递一个常用类型的数组对象,比如String[] strs、Integer[] ids之类的对象,最常见的操作,批量删除!同样,传递对象数组一样的道理,以对象为单位,比如VoStudent对象,包含两个域String name和int age,那么我们前端传JSON格式的字符串数组对象就好了。DEMO:var argument = '[{"age":"1","name":"李四"},{"age":"2","name":"张三"}]';而后端接收使用@RequestBody List<VoStudent> voStudnetList 来接收数据;

实现方案1):

<script type="text/javascript">
    //批量处理,inventoryListSubmit button id
    $('#inventoryListSubmit').click(function(){
        var studentArray= new Array();
        studentArray.push({age: "1", name: "李四"});
        studentArray.push({age: "2", name: "张三"});
        $.ajax({
               url: '${ctx}/abc/updateFormList',
            type: 'POST',
            data: JSON.stringify(studentArray),
            dataType:'json',
            contentType:'application/json;charset=utf-8',
            success: function (data) {
                if(data.status){
                    $.Pro(data.info);
                    setTimeout("goBack()",1000); 
                }else{
                    $.Pro('网络错误');
                }
                disabledBtn("#cancel");
            },
            error: function (data) {
                console.log(data.status);
            }
        });
    });
 
</script>

后台

或者如果不想创建实体类就直接用map进行接收

@RequestMapping("/iste")
	@ResponseBody
	public Map<String,Object> iste(@RequestBody List<Map> paramMap){
		Map<String,Object> resultMap = new HashMap<>();
		for (int i = 0; i < paramMap.size(); i++) {
			
		}
		resultMap.put("code", 0);
		resultMap.put("msg", "");
		return resultMap;
	}

这样就获取了前端传来的数组的对象

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: