首先在页面添加一个表单.
<form action="springmvc/testPojo" method="post">
<input type="text" name="username" />
<br>
<input type="text" name="email" />
<br>
<input type="text" name="address.city" />
<br>
<input type="text" name="address.provincial" />
<br>
<input type="text" name="age" />
<br>
<input type="submit" value="提交" />
</form>
在操作类中添加一个方法来接收这个表单.
@RequestMapping("/testPojo")
public String testPojo(User user){
System.out.println(user);
return SUCCESS;
}
另外需要添加一个User类,并添加GET和SET方法,为了后台显示清楚,重写toString方法。
该应用应该还包含级联模式,所以里面有一个Address类。
package com.yoni.springmvc.entities;
public class User {
private String username;
private String email;
private Address address;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [username=" + username + ", email=" + email + ", address=" + address + ", age=" + age + "]";
}
}
添加一个Address类,添加GET和SET方法,并重写toString方法。
package com.yoni.springmvc.entities;
public class Address {
private String city;
private String provincial;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvincial() {
return provincial;
}
public void setProvincial(String provincial) {
this.provincial = provincial;
}
@Override
public String toString() {
return "Address [city=" + city + ", provincial=" + provincial + "]";
}
}
将页面跑起来后,填写表单,提交后在后台可以看到如下结果。
User [username=aa, email=aa@aa.com, address=Address [city=wuhan, provincial=hubei], age=36]