一、基本数据类型的绑定
表单代码:
<form action="/echo" method="post"> <input name="name" value="张三" type="text"/> </form>
controller代码:
@RequestMapping(value = "/echo")
@ResponseBody
public String echo(String name){
return name;
}
注意:
- 方法形参中的参数名要和前台传进来的名一样,才能完成参数的绑定。
- 若不一样,我们可以使用@RequestParam注解标明传进来参数的名称,完成参数的绑定。
二、pojo类型的绑定
Model代码:
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
表单代码:
<form action="/save" method="post"> <input name="name" value="张三" type="text"/> <input name="age" value="21" type="text"/> </form>
Controller代码:
@RequestMapping("/save") public void test(User user) { }
三、数组和集合的绑定
1. 数组的绑定
表单代码:
<form action="/echoArray" method="post"> <input name="name" value="张三" type="text"/> <input name="name" value="李四" type="text"/> </form>
controller代码:
@RequestMapping(value = "/echoArray")
@ResponseBody
public String echo(String[] name){
return Arrays.toString(name);
}
2. List的绑定
SpringMVC不支持直接使用List接收参数,使用时,需封装在一个pojo类中
Model代码:
public class UserVo {
private List<User> userList;
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
表单代码:
<form action="/echoList" method="post"> <input name="userList[0].name" value="张三" type="text"/> <input name="userList[0].age" value="21" type="text"/> </form>
Controller代码:
@RequestMapping(value = "/echoList")
@ResponseBody
public String echoList(UserVo userVo){
List<User> userList = userVo.getUserList();
return JSON.toJSONString(userList);
}
3. Set的绑定
封装在pojo中,需手动创建对象,使用时下标不能超过Set集合的大小
public class UserVo { private Set<User> userSet = new HashSet<>(); public UserVo() { userSet.add(new User()); userSet.add(new User()); } public Set<User> getUserSet() { return userSet; } public void setUserSet(Set<User> userSet) { this.userSet = userSet; } }
表单代码:
<form action="/echoSet" method="post"> <input name="userList[0].name" value="张三" type="text"/> <input name="userList[0].age" value="21" type="text"/> </form>
Controller代码:
@RequestMapping(value = "/echoSet") @ResponseBody public String echoSet(UserVo userVo){ Set<User> userSet = userVo.getUserSet(); return JSON.toJSONString(userSet); }
4. Map的绑定
封装在pojo中
public class UserVo { private Map<String, User> userMap; public Map<String, User> getUserMap() { return userMap; } public void setUserMap(Map<String, User> userMap) { this.userMap = userMap; } }
表单代码:
<form action="/echoMap" method="post"> <input name="userMap['user1'].name" value="张三" type="text"/> <input name="userMap['user1'].age" value="21" type="text"/> </form>
Controller代码:
@RequestMapping(value = "/echoMap") @ResponseBody public String echoMap(UserVo userVo){ Map<String, User> userMap = userVo.getUserMap(); return JSON.toJSONString(userMap); }
四、json参数绑定
前台
$.ajax({ type: "POST", contentType: "application/json", dataType: "json", url: "/dataAnalysis/selsPoverty", data: JSON.stringify({ 'name': "张三", 'age': 21 }) , success: function (data) {
}
});
后台
@RequestMapping(value = "/echoJson") @ResponseBody public String echoJson(@RequestBody Map<String, Object> user){ return JSON.toJSONString(user); }