基础类型

原始类型: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

最新文章

  1. android的消息提示(震动与提示音)
  2. MySQL二进制安装
  3. UStore-添加自定义工作流(JDF)到产品
  4. c/c++面试题(1)
  5. c++ 模板元编程的一点体会
  6. C# AttributeUsage的使用浅析
  7. git 克隆项目 与 分支简单操作
  8. python报错
  9. transform属性
  10. Android Bitmap那些事之如何优化内存
  11. SQLserver关于教学楼中教室在某个时间段是否被占用的数据库设计
  12. Case Studies: Retail and Investment Banks Use of Social Media
  13. Xcode5.1离线下载安装及使用iOS5模拟器进行开发调试的方法
  14. 【转】EXT JS MVC开发模式
  15. php中使用curl两个例子
  16. Web开发中常用的状态码
  17. Activiti源代码分析
  18. linux文件访问权限(像rw-r--rw-是什么意思)
  19. break跳出循环的妙用
  20. C机器级移位,编码表示 无符号编码表示,有符号编码表示一般最常见的方式是补码

热门文章

  1. IDEA下调试和运行Hadoop程序例子
  2. 设计模式之Factory Method模式
  3. MongoDB中的读写锁
  4. 从9x9矩阵中抽取中间菱形区域打印 - perl
  5. html网页中如何给文字加入下划线
  6. laravel-admin挖坑之旅
  7. spring静态注入
  8. 【HQL】函数汇总
  9. 位运算骚操作 Part 3
  10. python大法好——飞机大战完整吧