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();
- …
- }
- }
相关推荐
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....
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...
注释的类标记为包含请求处理程序的Spring MVC的Bean @RestController -标记注释类为@Controller豆,并增加了@ResponseBody序列化返回的结果信息 @ Configuration-将带注释的类标记为定义bean的Java配置 @Service-...
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- apache.dbcp连接池的配置 --> ...
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 ...
2.找到相应的spring mvc配置文件 3.配置spring mvc文件 (springmvc-servlet.xml文件) (1)扫描基包下的所有注解类 (2)新建一个Student类 用于测试注解 (3)配置注解 @Controller @RequestMapping("/list") ...
Spring MVC在接收集合请求参数时,需要在Controller方法的集合参数里前添加@RequestBody,而@RequestBody默认接收的enctype (MIME编码)是application/json,因此发送POST请求时需要设置请求报文头信息,否则Spring ...
后台Controller: @RequestMapping(/faultType) @ResponseBody public Map<String> faultType(int id,HttpServletRequest request)throws IOException { String ReturnMessage = ; //获取所有子类故障类型 List...
mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in ...
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 ...
AngularJS是一个用JavaScript编写的,基于MVC的Web应用程序框架。 这样就可以在前端使用Model-View-Controller模式。 它还带有几个附加模块。 在此示例中,我还使用了angular-resource ,它是用于创建RE
jwx是开源的java公众号开发MVC框架,基于spring配置文件和微信消息或事件注解,通过微信上下文处理一个或多个微信公众号服务请求。目的主要有两个,其一生封装微信请求xml消息为java实体对象,将返回对象转换为xml...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!-- Support JSON --> <bean class="org.spring...
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.org.core.entity.User; ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
使用了MVC(Model-View-Controller)的设计模式,底层用的SpringBoot框架,将业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写...
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) ...
7:Spring MVC:分离模型、视图、控制器、以便更容易定制 折构函数和虚函数? 答:折构函数式销毁一个类的函数,虚函数是为了C++的动态绑定而设计的。 描述你的编程风格? 答:类名首字母大写,常量一般全部大写,...