zoukankan      html  css  js  c++  java
  • SpringMVC,SpringBoot使用ajax传递对象集合/数组到后台

    假设有一个bean名叫TestPOJO。

    1、使用ajax从前台传递一个对象数组/集合到后台。

    前台ajax写法:

    var testPOJO=new Array();
    //这里组装testPOJO数组
    $.ajax({
        url:“testController/testPOJOs”,
        data:JSON.stringify(testPOJO),
        type:"post",
        dataType:"json",
        contentType:"application/json",
        success:function (res) {
        },
        error:function(msg){ 
        }
    });

    后台接收方法:

    @RestController
    @RequestMapping("testController")
    public class testController {
    
      @RequestMapping("/testPOJOs")
      //如果类的注解是@Controller,那么方法上面还需要加@ResponseBody,因为@ResTController=@Controller+@ResponseBody
      public String testPOJOs (@RequestBody TestPOJO [] testPOJO) {
           //操作
      }
    
      //或者下面这个
      //@RequestMapping("/testPOJOs")
      //public String testPOJOs (@RequestBody List<TestPOJO> testPOJO) {
           //操作
      //}
    }

    无论是几维数组,前后台保持一致就行了。

    2、传递Map

    前台ajax写法:

    var testMap={
        "a":"aaa",
        "b":[1,2,3]
    };
    $.ajax({
        url:“testController/testMap”,
        data:JSON.stringify(testMap),
        type:"post",
        dataType:"json",
        contentType:"application/json",
        success:function (res) {
        },
        error:function(msg){
        }
    });
    

    后台接收方法:

    @RestController
    @RequestMapping("testController")
    public class testController {
     
      @RequestMapping("/testMap")
      public String testMap (@RequestBody Map<String,Object> map) {
           String a = (String) map.get("a");
           List<Integer> b = (List<Integer>) map.get("b");
           ...
      }
    }
    

    3、除了传递对象集合,还需要传递其他字段。

    前台ajax写法:

    var testPOJO=new Array();
    //这里组装testPOJO数组
    $.ajax({
        url:“testController/testPOJOs”,
        data:{
            “strs”: JSON.stringify(testPOJO),
            “others”,”…”
      },
        type:"post",
        dataType:"json",
        success:function (res) {
        },
        error:function(msg){ 
        }
    });

    后台接收方法:

    @RestController
    @RequestMapping("testController ")
    public class testController {
    
      @RequestMapping("/testPOJOs") 
      public String testPOJOs (String strs,String others) {
    
           //操作使用fastjson进行字符串对象转换
    
         List<TestPOJO> list=new ArrayList<>();
    
            JSONObject json =new JSONObject();
    
            JSONArray jsonArray= JSONArray.parseArray(strs);
    
            for(int i=0;i<jsonArray.size();i++){
    
              JSONObject jsonResult = jsonArray.getJSONObject(i);
    
              TestPOJO testPOJO=JSONObject.toJavaObject(jsonResult,TestPOJO.class);
    
                list.add(testPOJO);
            }
            //其他操作
      }
    } 

    或者直接把others和testPOJO数组重新组合一个新数组var arr=[testPOJO,”others的内容”],“strs”: JSON.stringify(arr),只传递一个strs字段就可以,然后后台转换。

    4、传递一个数组

    前台ajax写法:

     $.ajax({
    	url: 'testController/listByxxx',
    	data: {
    		"xxxs":xs//xs是一个数组
    	},
    	type: "post",
    	dataType: "json",
    	success: function (res) {}
     });
    

    后台接收方法:

     @RestController
     @RequestMapping("testController")
     public class testController {
    	 @RequestMapping("/listByxxx")
    	 public String listByxxx(@RequestParam(value = "xxxs[]")String[] xxxs){
    		 //操作
    	 }
     }
    

     @RequestBody一般用来处理Content-Type: application/x-www-form-urlencoded编码格式的数据。在GET请求中,不能使用@RequestBody。在POST请求,可以使用@RequestBody和@RequestParam。

  • 相关阅读:
    note:debugging requires the debug connect session system privilege
    jdk8 tomcat7
    idea git 整合使用
    connect: Address is invalid on local machine or port is not valid on remote
    mysql拒绝访问 Error 1044/1045 问题的解决
    mysql Error_code: 1593
    spring mvc 提交表单汉字乱码
    mysql 5.7.10 下互为主备配置
    Java开发笔记(七十四)内存溢出的两种错误
    Java开发笔记(七十三)常见的程序异常
  • 原文地址:https://www.cnblogs.com/jinghun/p/10397089.html
Copyright © 2011-2022 走看看