在springmvc中,如果将String类型和基本数据类型绑定到方法中,就必须保证表单中的"name"属性值或参数值要与要表单方法中的参数值一致,否则无法成功绑定(除非使用@RequestParam注解)。
测试绑定基本数据类型
@RequestMapping("testBindInteger")
public String testBindInteger(Integer money){
System.out.println(money);
return "success";
}
@RequestMapping("testBindString")
public String testBindString(String name){
System.out.println(name);
return "success";
}
在springmvc中如果将数据绑定(封装)到类对象中,则类对象的各属名称性必须与表单中"name"的属性值保持一致,并且类对象的各属性必须有set方法,否则,无法绑定(封装成功)。
public class Body {
private Integer id;
private String name;
private Float money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Body [id=" + id + ", name=" + name + ", money=" + money + "]";
}
}
@RequestMapping("/testBindBody")
public String testBindBody(Body body){
System.out.println(body);
return "success";
}
public class Address {
private String provinceName;
private String cityName;
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
@Override
public String toString() {
return "Address [provinceName=" + provinceName + ", cityName=" + cityName + "]";
}
}
public class Account {
private Integer id;
private String name;
private Float money;
private Address address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Account [id=" + id + ", name=" + name + ", money=" + money + ", address=" + address + "]";
}
}
@RequestMapping("/testBindAccount")
public String testBindAccount(Account account){
System.out.println(account);
return "success";
}
注:在springmvc中绑定类对象时,springmvc会自动调用类对象的set方法来将数据绑定(封装)到类对象中,所以类对象的属性名称与表单的"name"属性值必须保持一致。
private String userName;
private String password;
private Integer age;
private List<Body> bodyList;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Body> getBodyList() {
return bodyList;
}
public void setBodyList(List<Body> bodyList) {
this.bodyList = bodyList;
}
@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password + ", age=" + age + ", bodyList=" + bodyList
+ "]";
}
@RequestMapping("testBindUser")
public String testBindUser(User user){
System.out.println(user);
return "success";
}
import java.util.Map;
public class User {
private String userName;
private String password;
private Integer age;
private Map<String,Body> bodyMap;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Map<String, Body> getBodyMap() {
return bodyMap;
}
public void setBodyMap(Map<String, Body> bodyMap) {
this.bodyMap = bodyMap;
}
@Override
public String toString() {
return "User [userName=" + userName + ", password=" + password + ", age=" + age + ", bodyMap=" + bodyMap + "]";
}
}
@RequestMapping("testBindUser")
public String testBindUser(User user){
System.out.println(user);
return "success";
}
<a href="textServletAPI">访问原始的servletAPI对象a>
@RequestMapping("/textServletAPI")
public String textServletAPI(HttpServletRequest request,HttpServletResponse response){
System.out.println(request);
System.out.println(response);
System.out.println(request.getSession());
System.out.println(request.getServletContext());
return "success";
}