springMVC接收复杂集参数版权声明

原创
小哥 3年前 (2022-11-11) 阅读数 50 #大杂烩

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

  • 接收List设置参数:

1、页面js代码:

Js代码

  1. var idList = new Array();
  2. idList.push(“1”);
  3. idList.push(“2”);
  4. idList.push(“3”);
  5. var isBatch = false ;
  6. $.ajax({
  7. type: "POST" ,
  8. url: "<%=path%>/catalog.do?fn=deleteCatalogSchemes" ,
  9. dataType: json ,
  10. data: { "idList" :idList, "isBatch" :isBatch},
  11. success: function (data){
  12. },
  13. error: function (res){
  14. }
  15. });

2 、 Controller 方法:

Java代码

  1. @Controller

  2. @RequestMapping ( "/catalog.do" )

  3. public class CatalogController {

  4. @RequestMapping (params = "fn=deleteCatalogSchemes" )

  5. @ResponseBody

  6. public AjaxJson deleteCatalogSchemes( @RequestParam ( "idList[]" ) List idList,Boolean isBatch) {

  7. }

  8. }

  • 接收List、User[]设置参数:

1、User实体类:

Java代码

  1. public class User {
  2. private String name;
  3. private String pwd;
  4. //省略getter/setter
  5. }

2 、页面 js 代码:

Js代码

  1. var userList = new Array();
  2. userList.push({name: "李四" ,pwd: "123" });
  3. userList.push({name: "张三" ,pwd: "332" });
  4. $.ajax({
  5. type: "POST" ,
  6. url: "<%=path%>/catalog.do?fn=saveUsers" ,
  7. data: JSON.stringify(userList), //序列化对象JSON字符串
  8. dataType: "json" ,
  9. contentType : application/json;charset=utf-8 , //设置请求标头信息
  10. success: function (data){
  11. },
  12. error: function (res){
  13. }
  14. });

3 、 Controller 方法:

Java代码

  1. @Controller

  2. @RequestMapping ( "/catalog.do" )

  3. public class CatalogController {

  4. @RequestMapping (params = "fn=saveUsers" )

  5. @ResponseBody

  6. public AjaxJson saveUsers( @RequestBody List userList) {

  7. }

  8. }

如果您想接收 User[] 阵列,放好 saveUsers 的参数类型 @RequestBody User[] userArray 就行了。

  • 接收 List<Map<String,Object>>设置参数:

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

Js代码

  1. var userList = new Array();
  2. userList.push({name: "李四" ,pwd: "123" });
  3. userList.push({name: "张三" ,pwd: "332" });
  4. $.ajax({
  5. type: "POST" ,
  6. url: "<%=path%>/catalog.do?fn=saveUsers" ,
  7. data: JSON.stringify(userList), //序列化对象JSON字符串
  8. dataType: "json" ,
  9. contentType : application/json;charset=utf-8 , //设置请求标头信息
  10. success: function (data){
  11. },
  12. error: function (res){
  13. }
  14. });

2 、 Controller 方法:

Java代码

  1. @Controller

  2. @RequestMapping ( "/catalog.do" )

  3. public class CatalogController {

  4. @RequestMapping (params = "fn=saveUsers" )

  5. @ResponseBody

  6. public AjaxJson saveUsers( @RequestBody List<Map<String,Object>> listMap) {

  7. }

  8. }

  • 接收User(bean里面包含List)设置参数:

1、User实体类:

Java代码

  1. public class User {
  2. private String name;
  3. private String pwd;
  4. private List customers; //属于用户的客户群
  5. //省略getter/setter
  6. }

2 、页面 js 代码:

Js代码

  1. var customerArray = new Array();
  2. customerArray.push({name: "李四" ,pwd: "123" });
  3. customerArray.push({name: "张三" ,pwd: "332" });
  4. var user = {};
  5. user.name = "李刚" ;
  6. user.pwd = "888" ;
  7. user. customers = customerArray;
  8. $.ajax({
  9. type: "POST" ,
  10. url: "<%=path%>/catalog.do?fn=saveUsers" ,
  11. data: JSON.stringify(user), //序列化对象JSON字符串
  12. dataType: "json" ,
  13. contentType : application/json;charset=utf-8 , //设置请求标头信息
  14. success: function (data){
  15. },
  16. error: function (res){
  17. }
  18. });

3 、 Controller 方法:

Java代码

  1. @Controller

  2. @RequestMapping ( "/catalog.do" )

  3. public class CatalogController {

  4. @RequestMapping (params = "fn=saveUsers" )

  5. @ResponseBody

  6. public AjaxJson saveUsers( @RequestBody User user) {

  7. List customers = user.getCustomers();

  8. }

  9. }

原始地址: https://lihaiming.iteye.com/blog/2293840

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门