基础类型

原始类型:id必须要传,否则报错。

@RequestMapping("/test")
@ResponseBody
public ResponseData test(int id) {}

包装类型:id可以不传,后台接受到null。

@RequestMapping("/test")
@ResponseBody
public ResponseData test(Integer id) {}

list&set

简单类型

前台

form表单

<form action="${ctx}/test/test" method="post">
<input type="text" name="ids">
<input type="text" name="ids">
<input type="text" name="ids">
<input type="submit">
</form>

ajax

var data = [];
data.push(1);
data.push(2);
data.push(3);
$.ajax({
url: ctx + "/test/test",
traditional:true,//必要
data: {ids: data},
success: function (result) {
alert(result);
}
})

后台

@RequestMapping("/test")
@ResponseBody
public ResponseData test(@RequestParam List<Integer>ids) {}

复杂类型

list<User>users:(略)同json格式对象

数组

前台

form表单

<form action="${ctx}/test/test" method="post">
<input type="text" name="ids">
<input type="text" name="ids">
<input type="text" name="ids">
<input type="submit">
</form>

ajax

var data = [];
data.push(1);
data.push(2);
data.push(3);
$.ajax({
url: ctx + "/test/test",
traditional:true,//必要
data: {ids: data},
success: function (result) {
alert(result);
}
})

后台

@RequestMapping("/test")
@ResponseBody
public ResponseData test(Integer[]ids) {
}

map

前台

form

<form action="${ctx}/test/test" method="post">
<input type="text" name="name">
<input type="text" name="sex">
<input type="submit">
</form>

ajax

var data = {name:"zhangsan",sex:"man"};
$.ajax({
url: ctx + "/test/test",
data:data,
success: function (result) {
alert(result);
}
});

后台

@RequestMapping("/test")
@ResponseBody
public ResponseData test(@RequestParam Map<String,String> params) {}

pojo简单属性

前台

form

<form action="${ctx}/test/test" method="post">
<input type="text" name="name">
<input type="text" name="sex">
<input type="submit">
</form>

ajax

var data = {name:"zhangsan",sex:"man"};
$.ajax({
url: ctx + "/test/test",
data:data,
success: function (result) {
alert(result);
}
});

后台

@RequestMapping("/test")
@ResponseBody
public ResponseData test(User user) {}
public class User{
private String name;
private String sex;
//get and set ...
}

pojo包含list

前台

form

<form action="${ctx}/test/test" method="post">
<input type="text" name="userName" value="zhangsan">
<input type="text" name="sex" value="123">
<input type="text" name="posts[0].code" value="232"/>
<input type="text" name="posts[0].name" value="ad"/>
<input type="text" name="posts[1].code" value="ad"/>
<input type="text" name="posts[1].name" value="232"/>
<input type="submit">
</form>

ajax

var user={userName:"zhangsan",password:"123"};
user['posts[0].name']="ada";
user['posts[0].code']="ada";
user['posts[1].name']="ad";
user['posts[1].code']="ad2323a";
$.ajax({
url: ctx + "/test/test",
type:"post",
contentType: "application/x-www-form-urlencoded",
data:user,
success: function (result) {
alert(result);
}
});

后台

public class User{
private String name;
private String sex;
private List<Post> posts;
//get and set ...
}
public class Post {
private String code;
private String name;
//get and set ...
}
@RequestMapping("/test")
@ResponseBody
public ResponseData test(User user) {}

date类型

使用注解方式

绑定单个方法

对于传递参数为Date类型,可以在参数前添加@DateTimeFormat注解。如下:

@RequestMapping("/test")
public void test(@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") Date date){}

如果传递过来的是对象,可以在对象属性上添加注解。

@RequestMapping("/test")
public void test(Person person){} Public class Person{
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
Private Date date;
Private String name;
}
绑定整个controller的所有方法:
@Controller
public class FormController { @InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
}
@Controller
public class FormController {
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
}
}

使用PropertyEditor方式

使用ConversionService方式

参考:

https://www.2cto.com/kf/201501/374062.html

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-initbinder

枚举类型

mvc配置文件添加:

 <!--枚举类型转化器-->
<bean id="formattingConversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="org.springframework.core.convert.support.StringToEnumConverterFactory"/>
</set>
</property>
</bean>

参考:

Spring Boot绑定枚举类型参数

Spring MVC 自动为对象注入枚举类型

json格式对象

后台

@RequestMapping(value = "introductionData.do", method = {RequestMethod.POST})
@ResponseBody
public RestResult introductionData(@RequestBody SignData signData) {
}
public class SignData {
private ApplicationInformation applicationInformation;
private ApplyUserInformation applyUserInformation;
private StudentInformation studentInformation;
private List<VolunteerItem> volunteerItems;
private ResidencePermit residencePermit;
private List<Family> families;
//get and set ...
}

前台

var data = {}
var applyUserInformation = {
"applyName": APPLYNAME,
"applyCardType": "0",
"applyCardID": APPLYIDCARD,
"applyMobile": APPLYMOBILE
};
var applicationInformation = {
"live_address": nowaddress,
"addressJZArea": addressJZArea,
"schoolLevel": schoolLevel,
"schoolType": schoolType,
"applyName": APPLYNAME,
"contacts": contacts,
"contactNumber": contactNumber
};
var studentInformation = {
"cardType": cardType,
"cardID": cardID,
"studentName": studentName,
"studentType": studentType,
"studentType1": studentSpecialCase,
"isDisability": "0",
"studentCategory": "0",
"birthday":birthday,
"graduationschool":SchoolName,
"graduationclass":classNameinfo,
"applyName": APPLYNAME
};
data["applyUserInformation"] = applyUserInformation;
data["applicationInformation"] = applicationInformation;
data["studentInformation"] = studentInformation; $.ajax({
url: ctx + '/overseasData.do',
type: "post",
data: JSON.stringify(data),
contentType: "application/json;charset=utf-8",
success: function (result) {}
})

参考:https://blog.csdn.net/qq_29663071/article/details/68922043

spring mvc 参数绑定的更多相关文章

  1. spring mvc参数绑定

    spring绑定参数的过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接 ...

  2. Spring MVC参数绑定(如何接收请求参数及返回参数)

    在SpringMVC interceptor案例实践中遇到了获取jsp表单传递参数失败的问题,怎么的解决的呢?下面详细介绍. 先讲述下https://www.cnblogs.com/ilovebath ...

  3. spring mvc: 资源绑定视图解析器(不推荐)

    spring mvc: 资源绑定视图解析器(不推荐) 不适合单控制器多方法访问,有知道的兄弟能否告知. 访问地址: http://localhost:8080/guga2/hello/index 项目 ...

  4. spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping

    spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...

  5. Spring MVC资源绑定视图解析器

    ResourceBundleViewResolver使用属性文件中定义的视图bean来解析视图名称. 以下示例显示如何使用Spring Web MVC框架中的ResourceBundleViewRes ...

  6. Spring MVC 参数的绑定方法

    在Spring MVC中,常见的应用场景就是给请求的Url绑定参数.本篇就介绍两种最最基本的绑定参数的方式: 基于@RequestParam 这种方法一般用于在URL后使用?添加参数,比如: @Req ...

  7. spring自定义参数绑定(日期格式转换)

    spring参数绑定时可能出现 BindException(参数绑定异常),类似下面的日期绑定异常(前台传过来是String类型,实际的pojo是Date类型) default message [Fa ...

  8. Spring MVC参数处理

    使用Servlet API作为参数 HttpServletRequest HttpServletResponse HttpSession 使用流作为参数 总结 Spring MVC通过分析处理处理方法 ...

  9. spring mvc 参数

    Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts: Struts是一个表示层框架,主要作用是界面展示,接收请求,分发请求. 在MVC框架中,Struts属于V ...

随机推荐

  1. SpringCloud注解和配置以及pom依赖说明

    在本文中说明了pom依赖可以支持什么功能,以及支持什么注解,引入该依赖可以在application.properties中添加什么配置. 1.SpringCloud 的pom依赖 序号 pom依赖 说 ...

  2. VMware与Centos系统安装

    Linux介绍 1. Linux Linux和windows一样都是操作系统,Linux是开源的.免费的.自由传播的类Unix操作系统软件. 是一个基于POSIX和UNIX的多用户.多任务.支持多线程 ...

  3. LeetCode【108. 将有序数组转换为二叉搜索树】

    又是二叉树,最开始都忘记了二叉搜索树是什么意思,搜索了一下: 二叉搜索树:左节点都小于右节点,在这里就可以考虑将数组中的中间值作为根节点 平衡二叉树:就是左右节点高度不大于1 树就可以想到递归与迭代, ...

  4. idea中使用MyBatis Generator

    1.新建maven项目 2.新建Generator配置文件 generator_config.xml <?xml version="1.0" encoding="U ...

  5. Mybatis常见疑问

    1.在连接数据库时候,mysql是否支持fetchsize分页获取? 满足以下几个条件,可以使用fetchsize,根据游标获得记录 ①MySQL 从5.0.2开始支持分页获得. ②同时需要在jdbc ...

  6. Redhat Linux 配置Xmanager

    1. vi /etc/inittab id:5:initdefault:  //设置系统运行级为5,如果本来就是5就无需修改 id:5:respawn:/usr/sbin/gdm    //添加到最后 ...

  7. RIDE创建工程和测试套件和用例--书本介绍的入门方法,自己整理实践下

    1.选择File->New Project 2.弹出的New Project对话框,在Name文本框输入一个名词,如“TestProject-0805”,右侧选中“Directory”,选中建立 ...

  8. Filter简易实现.

    一 代码结构: 二 代码 Test.java: package com.demo.test; import com.demo.filter.ApplicationFilterChain; import ...

  9. 转:Eclipse中web项目部署至Tomcat步骤

    原址:http://blog.csdn.net/lucklq/article/details/7621807 Eclipse的web工程至Tomcat默认的部署目录是在工程空间下,本文旨在将部署目录改 ...

  10. CCF2017-9-1

    题目: 分析:将所有可能列出来,80可以分为8个10块(这时候最多也是8个),在这种情况下,可以分为2个30块, 3个循环嵌套,判断是不是输入的总钱数,因为不是所有的都是加起来是80,就是分为买多少个 ...