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。


免責聲明!

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



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