spring mvc 參數綁定
阿新 • • 發佈:2018-11-12
format ctx param stp lca date類型 evel blog ram
基礎類型
原始類型: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 參數綁定