ajax传递list集合

 

一:ajax传递List<String>类型的数据html

js代码:java

[html] view plain copy
  1. //声明list  
  2. var _list = [];  
  3. //放入string对象  
  4. for (var i = 0; i < 3; i++) {  
  5.     _list[i]="tom";  
  6. }  
  7.   
  8. $.ajax({  
  9.     url : '/ajax/test',  
  10.     data : "list="+_list,  
  11.     type : "POST",  
  12.     success : function(data) {  
  13.         alert(data);  
  14.     }  
  15. });  


java代码:ajax

[html] view plain copy
  1. @RequestMapping(value="test",method=RequestMethod.POST)  
  2. @ResponseBody  
  3. public String ajaxList(@RequestParam("list")List<String> strList){  
  4.           
  5.     for (String str : strList) {  
  6.         System.out.println(str);  
  7.     }  
  8.     return "OK";  
  9. }  

 

二:ajax传递List<Obj>类型的数据json

后台须要用到json解析工具,我选得是jacksonapp

导入jackson依赖:ide

[html] view plain copy
  1. <dependency>  
  2.     <groupId>com.fasterxml.jackson.core</groupId>  
  3.     <artifactId>jackson-databind</artifactId>  
  4.     <version>2.7.3</version>  
  5. </dependency>  


js代码:工具

[html] view plain copy
  1. //声明list  
  2. var _list = [];  
  3. //建立两个user对象  
  4. var a= {};  
  5. a.name="tom";  
  6. a.age=23;  
  7. a.city="上海";  
  8. var b = {};  
  9. b.name="jack";  
  10. b.age=25;  
  11. a.city="安徽";  
  12. //将user放入_list  
  13. _list.push(a);  
  14. _list.push(b);  
  15.   
  16. $.ajax({  
  17.     url : '/ajax/test1',  
  18.     data : "list="+JSON.stringify(_list),  
  19.     type : "POST",  
  20.     success : function(data) {  
  21.         alert(data);  
  22.     }  
  23. });  

 

java代码:url

[html] view plain copy
  1. @RequestMapping(value="test",method=RequestMethod.POST)  
  2. @ResponseBody  
  3. public String ajaxList(@RequestParam("list")String userList) throws Exception{  
  4.     //jackson对象  
  5.     ObjectMapper mapper = new ObjectMapper();  
  6.     //使用jackson将json转为List<User>  
  7.     JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, User.class);     
  8.     List<User> list =  (List<User>)mapper.readValue(userList, jt);  
  9.           
  10.     return "OK";  
  11. }  

 

三:当ajax传递任何复杂参数时,后台能够直接从流中来读取数据进行解析xml

js代码:htm

[html] view plain copy
  1. //声明list  
  2. var _list = [];  
  3. //建立两个user对象  
  4. var a= {};  
  5. a.name="tom";  
  6. a.age=23;  
  7. a.city="上海";  
  8. var b = {};  
  9. b.name="jack";  
  10. b.age=25;  
  11. a.city="安徽";  
  12. //将user放入_list  
  13. _list.push(a);  
  14. _list.push(b);  
  15.   
  16. $.ajax({  
  17.     url : '/querz/test',  
  18.     data : JSON.stringify(_list),//这里须要json化  
  19.     type : "POST",  
  20.     success : function(data) {  
  21.         alert(data);  
  22.     }  
  23. });  


java代码:

[html] view plain copy
  1. @RequestMapping(value="test",method=RequestMethod.POST)  
  2. @ResponseBody  
  3. public String ajaxList(HttpServletRequest request) throws Exception{  
  4.       
  5.     //从流中读取数据  
  6.     BufferedReader br = request.getReader();  
  7.     String str = "";  
  8.     StringBuffer sb = new StringBuffer();  
  9.     while((str = br.readLine()) != null){  
  10.         sb.append(str);  
  11.     }  
  12.           
  13.     ObjectMapper mapper = new ObjectMapper();  
  14.     //使用jackson解析数据  
  15.     JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, User.class);     
  16.     List<User> list =  (List<User>)mapper.readValue(sb.toString(), jt);   
  17.     System.out.println(list);  
  18.           
  19.     return "OK";  
相关文章
相关标签/搜索