1.复杂的javabean
(1)javaBean
public class Spitter {
private long id;
@NotNull
@Size(min = 5,max = 16,message = "{username.size}")
private String username;
@NotNull
@Size(min = 5,max = 25,message = "{password.size}")
private String password;
@NotNull(message = "{firstName.null}")
@Size(min=2,max=30,message = "{firstName.size}")
private String firstName;
@NotNull
@Size(min=2,max=30,message = "{lastName.size}")
private String lastName;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
(2).controller
@RequestMapping("/receive")
public String receiveDate3(@RequestBody List<Spitter> spitters){
System.out.println(spitters);
return "redirect:/index.jsp";
}
或
@RequestMapping("/receive")
public String receiveDate3(@RequestBody Spitter[] spitters){
System.out.println(spitters);
return "redirect:/index.jsp";
}
(3).json格式
[
{
"id":"1",
"firstName":"first1",
"lastName":"last1",
"username":"user1",
"password":"pwd1"
},
{
"id":"2",
"firstName":"first2",
"lastName":"last2",
"username":"user2",
"password":"pwd2"
},
{
"id":"3",
"firstName":"first3",
"lastName":"last3",
"username":"user3",
"password":"pwd3"
}
]
(4).前台js 封装json
var spitters=[];
for(var i=1;i<tr.length;i++){
var spitter={};
spitters.id=id;
spitters.username=username;
spitters.password=password;
spitters.firstName=firstName;
spitters.lastName=lastName;
spitters.push(spitter);
}
//spring mvc接收数组对象 要传json 格式[{key:value,key,value},{key:value,key,value}]
var json=JSON.stringify(spitters);
(5)
2.对于基本类型
(1).controller
@RequestMapping("/ids")
public String receiveDate(int [] ids){
System.out.println(ids);
return "redirect:/index.jsp";
}
(2)url
http://localhost:9091?ids=1&ids=2&ids=3
(3)
如果上述controller采用List<Integer>ids 作为参数,会报错: