Allen_Zsj

导航

StringMVC POJO

SpringMVC会按请求参数名和POJO类属性名进行自动匹配,自动为该属性填充属性值,

支持级联属性(本类包含其他类对象,如User类有一个属性为Address)

示例代码:

index.jsp:

<form action="springmvc/testPOJO" method="post">
username:<input type="text" name="username"/>
age:<input type="text" name="age"/>
password:<input type="password" name="password"/>
privnce:<input type="text" name="address.privnce"/>
city:<input type="text" name="address.city"/>
<input type="submit" value="Submit"/>
</form>

User.java:

package com.allen.entities;

public class User {
// username:<input type="text" value="username"/>
// age:<input type="text" value="age"/>
// password:<input type="password" value="password"/>
// privnce:<input type="text" value="privnce"/>
// city:<input type="text" value="city"/>
private String username;
private int age;
private String password;
private Address address;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

@Override
public String toString() {
return "User [username=" + username + ", age=" + age + ", password="
+ password + ", address=" + address + "]";
}

}

Address.java:

package com.allen.entities;

public class Address {
private String privnce;
private String city;

public String getPrivnce() {
return privnce;
}

public void setPrivnce(String privnce) {
this.privnce = privnce;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

@Override
public String toString() {
return "Address [privnce=" + privnce + ", city=" + city + "]";
}

}

@Controller:

@RequestMapping("/testPOJO")
public String testPOJO(User user){
System.out.println("testPOJO user: " + user);
return SUCCESS;

}

 

posted on 2017-12-21 14:19  Allen_Zsj  阅读(168)  评论(0编辑  收藏  举报