zoukankan      html  css  js  c++  java
  • SpringMVC接收复杂集合参数,集合对象

    Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是application/json,因此发送POST请求时需要设置请求报文头信息,否则Spring MVC在解析集合请求参数时不会自动的转换成JSON数据再解析成相应的集合。以下列举接收List<Integer>、List<User>、List<Map<String,Object>>、User[]、User(bean里面包含List)几种较为复杂的集合参数示例:

    • 一、接收List<Integer>集合参数:

    1、页面js代码:

    Js代码

    var arr = [1,2,3];
    $.jBox.confirm("确定要删除数据吗?", "warning",function () {
        $.ajax({
           type: 'get',
           url: '${base.contextPath}/giving/index/del',
           dataType: 'json',
           data: {ids: arr},
           success: function (result) {
              …
           },
           error: function (result) {
              …
           }
       })
    })

    2、Controller方法:

    Java代码

    @Controller
    @RequestMapping("/wxgiving")
    public class WxGivingController{
      @RequestMapping(value = "/index/del", method = RequestMethod.GET)
      @ResponseBody
      public ReturnMsg del (@RequestParam(value = "ids[]")List <Integer> ids){
         …
      }
    }
    •  接收List<User>、User[]集合参数:

    1、User实体类:

    Java代码

    public class User {
      private int id;
      private String name;
      private String pwd;
      //省略getter/setter
    }

    2、页面js代码:

    Js代码

    //可以找前端拼成这种类型
    var userList = new Array();
    userList.push({name: "张三",pwd: "123"});
    userList.push({name: "李四",pwd: "223"});
    $.ajax({
        type: "POST",
        url: "${base.contextPath}/user/index/add",
        data: JSON.stringify(userList),//将对象序列化成JSON字符串
        dataType:"json",
        contentType : 'application/json;charset=utf-8', //设置请求头信息
        success: function(result){
            …
        },
        error: function(result){
            …
        }
      });

    3、Controller方法:

    Java代码

    @Controller
    @RequestMapping(value = "/user")
    public class UserController(){
      @RequestMapping(value = "/index/add", method = RequestMethod.POST)
      @ResponseBody
      public ReturnMsg addOrEdit(@RequestBody List<User> userList) {
         …
      }
    }

    如果想要接收User[]数组,只需要把add的参数类型改为@RequestBody User[] userArray就行了。

    • 接收List<Map<String,Object>>集合参数:

    1、页面js代码(不需要User对象了):

    Js代码

    1. var userList = new Array();
      userList.push({name: "张三",pwd: "123"});
      userList.push({name: "李四",pwd: "223"});
      $.ajax({
          type: "POST",
          url: "${base.contextPath}/user/index/add",
          data: JSON.stringify(userList),//将对象序列化成JSON字符串
          dataType:"json",
          contentType : 'application/json;charset=utf-8', //设置请求头信息
          success: function(result){
              …
          },
          error: function(result){
              …
          }
        });

    2、Controller方法:

    Java代码

    1. @Controller
      @RequestMapping(value = "/user")
      public class UserController(){
        @RequestMapping(value = "/index/add", method = RequestMethod.POST)
        @ResponseBody
        public ReturnMsg addOrEdit(@RequestBody List<Map<String,Object>> listMap) {
          …
        }
      }
    •  接收User(bean里面包含List)集合参数:

    1、User实体类:

    Java代码

    1. public class User {
        private int id;
        private String name;
        private String pwd;
        private List<User> userList;
        //省略getter/setter
      }

    2、页面js代码:

    Js代码

    1. var userArray= new Array();
      userArray.push({name: "张三",pwd: "123"});
      userArray.push({name: "李四",pwd: "223"});
      var user = {};
      user.name = "王五";
      user.pwd = "888";
      user.userList= userArray;
      $.ajax({
          type: "POST",
          url: "${base.contextPath}/user/index/add",
          data: JSON.stringify(user),//将对象序列化成JSON字符串
          dataType:"json",
          contentType : 'application/json;charset=utf-8', //设置请求头信息
          success: function(result){
              …
          },
          error: function(result){
              …
          }
        });

    3、Controller方法:

    Java代码

    1. @Controller
      @RequestMapping(value = "/user")
      public class UserController(){
        @RequestMapping(value = "/index/add", method = RequestMethod.POST)
        @ResponseBody
        public ReturnMsg addOrEdit(@RequestBody User user) {
          List<User> userList= user.getUserList();
  • 相关阅读:
    一个很好的在线测试编辑器(可以在线运行很多程序)
    基于angular的route实现单页面cnodejs
    微博
    jsonp跨域再谈
    打开IIS的快捷键
    PHPCMS笔记第二弹
    phpcms ——模板标签详细使用说明
    PHP流程管理,堪比小小程序
    PHP的简单易懂文件管理,可实现基本功能
    使用php ajax写省、市、区、三级联动
  • 原文地址:https://www.cnblogs.com/yangchuncool/p/9244190.html
Copyright © 2011-2022 走看看