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代码:
- var idList = new Array();
- idList.push(“1”);
- idList.push(“2”);
- idList.push(“3”);
- var isBatch = false;
- $.ajax({
- type: "POST",
- url: "<%=path%>/catalog.do?fn=deleteCatalogSchemes",
- dataType: 'json',
- data: {"idList":idList,"isBatch":isBatch},
- success: function(data){
- …
- },
- error: function(res){
- …
- }
- });
2、Controller方法:
- @Controller
- @RequestMapping("/catalog.do")
- public class CatalogController {
- @RequestMapping(params = "fn=deleteCatalogSchemes")
- @ResponseBody
- public AjaxJson deleteCatalogSchemes(@RequestParam("idList[]") List<String> idList,Boolean isBatch) {
- …
- }
- }
- 接收List<User>、User[]集合参数:
1、User实体类:
- public class User {
- private String name;
- private String pwd;
- //省略getter/setter
- }
2、页面js代码:
- var userList = new Array();
- userList.push({name: "李四",pwd: "123"});
- userList.push({name: "张三",pwd: "332"});
- $.ajax({
- type: "POST",
- url: "<%=path%>/catalog.do?fn=saveUsers",
- data: JSON.stringify(userList),//将对象序列化成JSON字符串
- dataType:"json",
- contentType : 'application/json;charset=utf-8', //设置请求头信息
- success: function(data){
- …
- },
- error: function(res){
- …
- }
- });
3、Controller方法:
- @Controller
- @RequestMapping("/catalog.do")
- public class CatalogController {
- @RequestMapping(params = "fn=saveUsers")
- @ResponseBody
- public AjaxJson saveUsers(@RequestBody List<User> userList) {
- …
- }
- }
如果想要接收User[]数组,只需要把saveUsers的参数类型改为@RequestBody User[] userArray就行了。
- 接收List<Map<String,Object>>集合参数:
1、页面js代码(不需要User对象了):
- var userList = new Array();
- userList.push({name: "李四",pwd: "123"});
- userList.push({name: "张三",pwd: "332"});
- $.ajax({
- type: "POST",
- url: "<%=path%>/catalog.do?fn=saveUsers",
- data: JSON.stringify(userList),//将对象序列化成JSON字符串
- dataType:"json",
- contentType : 'application/json;charset=utf-8', //设置请求头信息
- success: function(data){
- …
- },
- error: function(res){
- …
- }
- });
2、Controller方法:
- @Controller
- @RequestMapping("/catalog.do")
- public class CatalogController {
- @RequestMapping(params = "fn=saveUsers")
- @ResponseBody
- public AjaxJson saveUsers(@RequestBody List<Map<String,Object>> listMap) {
- …
- }
- }
- 接收User(bean里面包含List)集合参数:
1、User实体类:
- public class User {
- private String name;
- private String pwd;
- private List<User> customers;//属于用户的客户群
- //省略getter/setter
- }
2、页面js代码:
- var customerArray = new Array();
- customerArray.push({name: "李四",pwd: "123"});
- customerArray.push({name: "张三",pwd: "332"});
- var user = {};
- user.name = "李刚";
- user.pwd = "888";
- user. customers = customerArray;
- $.ajax({
- type: "POST",
- url: "<%=path%>/catalog.do?fn=saveUsers",
- data: JSON.stringify(user),//将对象序列化成JSON字符串
- dataType:"json",
- contentType : 'application/json;charset=utf-8', //设置请求头信息
- success: function(data){
- …
- },
- error: function(res){
- …
- }
- });
3、Controller方法:
- @Controller
- @RequestMapping("/catalog.do")
- public class CatalogController {
- @RequestMapping(params = "fn=saveUsers")
- @ResponseBody
- public AjaxJson saveUsers(@RequestBody User user) {
- List<User> customers = user.getCustomers();
- …
- }
- }
分享到:
Java性能优化之——JAVA内存优化编程的三个 ...
- 2014-05-27 18:08
- 浏览 31704
- 评论(9)
- 收藏
- 分类:Web前端
- 相关推荐
评论
9 楼 lvxing823533851 2016-03-23 引用
UserBean: id,username
Map<String,List<UserBean>> userMap 如果是这种类型,jsp里面该怎么写?
我想通过提交表单的方式,使用form.serialize()方式提交,name="userMap[0].[0].username"这里应该怎么写!
Map<String,List<UserBean>> userMap 如果是这种类型,jsp里面该怎么写?
我想通过提交表单的方式,使用form.serialize()方式提交,name="userMap[0].[0].username"这里应该怎么写!
8 楼 Mozilla大叔 2016-02-01 引用
HTTP Status 415 -
type Status report
message
description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
Apache Tomcat/6.0.29
我用的 接收List<User>、User[]集合参数 这种,但页面一直提示上边的415,后台没有报错,请问这种什么原因啊,还请帮忙回答一下。
type Status report
message
description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
Apache Tomcat/6.0.29
我用的 接收List<User>、User[]集合参数 这种,但页面一直提示上边的415,后台没有报错,请问这种什么原因啊,还请帮忙回答一下。
7 楼 springjpa_springmvc 2015-11-11 引用
spring mvc demo教程源代码下载,地址:
http://www.zuidaima.com/share/kspringmvc-p1-s1.htm
http://www.zuidaima.com/share/kspringmvc-p1-s1.htm
6 楼 finemi 2015-08-10 引用
确实可以!!
5 楼 whyzh 2015-05-13 引用
我试了一下,都不能用。。。。
如果在contraller里是放的是对象,js传参数的时候是对象,那么参数将一个也没有收到;如果js里放的参数是一个一个属性值,就可以接收到。。。
如果穿List,报错。。神马转换的时候就出错了。。。。不知道咋回事
如果在contraller里是放的是对象,js传参数的时候是对象,那么参数将一个也没有收到;如果js里放的参数是一个一个属性值,就可以接收到。。。
如果穿List,报错。。神马转换的时候就出错了。。。。不知道咋回事
4 楼 programmer__ 2015-02-08 引用
能帮我看看嘛?
3 楼 programmer__ 2015-02-08 引用
严重: Servlet.service() for servlet [spring] in context with path [/zk] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.zk.model.device.Modem] with root cause
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.zk.model.device.Modem
at com.zk.action.device.OltController.oltSelect(OltController.java:108)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.zk.model.device.Modem
at com.zk.action.device.OltController.oltSelect(OltController.java:108)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
2 楼 programmer__ 2015-02-08 引用
为什么我按照你的方法传参数的时候List<Map<String,Object>>能用,List<User>不能用?
1 楼 w156445045 2015-01-08 引用
而@RequestBody默认接收的enctype (MIME编码)是application/json
不会吧,默认接受的应该是html类型吧
明天试试这种绑定。
不会吧,默认接受的应该是html类型吧
明天试试这种绑定。
参考自: http://jxd-zxf.iteye.com/blog/2072300/
参考知识库