`

Spring mvc list controller

 
阅读更多

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

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

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.     },  
  14.     error: function(res){  
  15.         …  
  16.     }  
  17. });  

 

 2Controller方法:

 

Java代码  收藏代码
  1. @Controller  
  2. @RequestMapping("/catalog.do")  
  3. public class CatalogController {  
  4.   
  5.     @RequestMapping(params = "fn=deleteCatalogSchemes")  
  6.     @ResponseBody  
  7.     public AjaxJson deleteCatalogSchemes(@RequestParam("idList[]") List<String> idList,Boolean isBatch) {  
  8.             …  
  9.     }  
  10. }  

 

  •  接收List<User>、User[]集合参数:

 1User实体类:

 

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.     },  
  13.     error: function(res){  
  14.         …  
  15.     }  
  16. });  

 

3Controller方法:

Java代码  收藏代码
  1. @Controller  
  2. @RequestMapping("/catalog.do")  
  3. public class CatalogController {  
  4.   
  5.     @RequestMapping(params = "fn=saveUsers")  
  6.     @ResponseBody  
  7.     public AjaxJson saveUsers(@RequestBody List<User> userList) {  
  8.         …  
  9.     }  
  10. }  

    如果想要接收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.     },  
  13.     error: function(res){  
  14.         …  
  15.     }  
  16. });  

  

2Controller方法:

Java代码  收藏代码
  1. @Controller  
  2. @RequestMapping("/catalog.do")  
  3. public class CatalogController {  
  4.   
  5.     @RequestMapping(params = "fn=saveUsers")  
  6.     @ResponseBody  
  7.     public AjaxJson saveUsers(@RequestBody List<Map<String,Object>> listMap) {  
  8.         …  
  9.     }  
  10. }  

 

  •  接收User(bean里面包含List)集合参数:

 1User实体类:

Java代码  收藏代码
  1. public class User {  
  2.     private String name;   
  3.     private String pwd;  
  4.     private List<User> 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.     },  
  17.     error: function(res){  
  18.         …  
  19.     }  
  20. });  

  3Controller方法:

Java代码  收藏代码
  1. @Controller  
  2. @RequestMapping("/catalog.do")  
  3. public class CatalogController {  
  4.   
  5.     @RequestMapping(params = "fn=saveUsers")  
  6.     @ResponseBody  
  7.     public AjaxJson saveUsers(@RequestBody User user) {  
  8.         List<User> customers = user.getCustomers();  
  9.         …  
  10.     }  
  11. }  
分享到:
评论

相关推荐

    Spring MVC 入门实例

    9 import org.springframework.web.servlet.mvc.Controller; 10 import org.springframework.web.servlet.ModelAndView; 11 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet....

    spring_MVC源码

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...

    spring-boot-annotation-list:Spring Boot应用程序中常用注解的精选列表

    注释的类标记为包含请求处理程序的Spring MVC的Bean @RestController -标记注释类为@Controller豆,并增加了@ResponseBody序列化返回的结果信息 @ Configuration-将带注释的类标记为定义bean的Java配置 @Service-...

    springjdbc

    &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; &lt;!-- apache.dbcp连接池的配置 --&gt; ...

    Spring-Reference_zh_CN(Spring中文参考手册)

    2.5.1. Spring MVC的表单标签库 2.5.2. Spring MVC合理的默认值 2.5.3. Portlet 框架 2.6. 其他特性 2.6.1. 动态语言支持 2.6.2. JMX 2.6 .3. 任务规划 2.6.4. 对Java 5(Tiger)的支持 2.7. 移植到Spring 2.0 ...

    springmvcwendang

    2.找到相应的spring mvc配置文件 3.配置spring mvc文件 (springmvc-servlet.xml文件) (1)扫描基包下的所有注解类 (2)新建一个Student类 用于测试注解 (3)配置注解 @Controller @RequestMapping("/list") ...

    spirngmvc js传递复杂json参数到controller的实例

    Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是application/json,因此发送POST请求时需要设置请求报文头信息,否则Spring ...

    Spring MVC中Ajax实现二级联动的简单实例

    后台Controller: @RequestMapping(/faultType) @ResponseBody public Map&lt;String&gt; faultType(int id,HttpServletRequest request)throws IOException { String ReturnMessage = ; //获取所有子类故障类型 List...

    springmybatis

    mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in ...

    thymeleaf-extras-springsecurity-3.0-master.zip

    In order to use the thymeleaf-extras-springsecurity3 or thymeleaf-extras-springsecurity4 modules in our Spring MVC application, we will first need to configure our application in the usual way for ...

    list-spring-angular:使用SpringBoot MVC和Angular的示例应用程序

    AngularJS是一个用JavaScript编写的,基于MVC的Web应用程序框架。 这样就可以在前端使用Model-View-Controller模式。 它还带有几个附加模块。 在此示例中,我还使用了angular-resource ,它是用于创建RE

    java微信公众号MVC开发框架

    jwx是开源的java公众号开发MVC框架,基于spring配置文件和微信消息或事件注解,通过微信上下文处理一个或多个微信公众号服务请求。目的主要有两个,其一生封装微信请求xml消息为java实体对象,将返回对象转换为xml...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    springMVC3使用@ResponseBody向浏览器返回 json,注意区分jar包前缀

    &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt; &lt;property name="messageConverters"&gt; &lt;list&gt; &lt;!-- Support JSON --&gt; &lt;bean class="org.spring...

    SpringMVC-SSH全注解

    import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.org.core.entity.User; ...

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    人工智能-项目实践-搜索引擎-基于Elasticsearch,Vue,Springboot和Web爬虫的仿豆瓣电影搜索引擎

    使用了MVC(Model-View-Controller)的设计模式,底层用的SpringBoot框架,将业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写...

    java面试宝典

    194、In a architecture pattern like MVC, it is mandatory that Servlet should be the controller, why not JSP? 46 195、Why JSP is used as View part of MVC ? 46 196、Can a database connection pool be ...

    单点登录源码

    SpringMVC | MVC框架 | [http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc](http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc) ...

    java面试题

    7:Spring MVC:分离模型、视图、控制器、以便更容易定制 折构函数和虚函数? 答:折构函数式销毁一个类的函数,虚函数是为了C++的动态绑定而设计的。 描述你的编程风格? 答:类名首字母大写,常量一般全部大写,...

Global site tag (gtag.js) - Google Analytics