package entity;
public class Address {
private String homeAddress;
private String schoolAddress;
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public String getSchoolAddress() {
return schoolAddress;
}
public void setSchoolAddress(String schoolAddress) {
this.schoolAddress = schoolAddress;
}
}
package entity;
public class Student {
private int id;
private String name;
private Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
@RequestMapping("welcome2")//拦截请求 public String
public String welcome2(Student student ) {
System.out.println(student.getId()+student.getName()+student.getAddress().getHomeAddress()
+student.getAddress().getSchoolAddress());
return "success";
}
<form action="SpringMVCService/welcome2">
年龄:<input type="text" name="id" value="1710252276"><br>
用户名:<input type="text" name="name" value="海棠"><br>
家庭地址:<input type="text" name="address.homeAddress" value="漯河"><br>
学校地址:<input type="text" name="address.schoolAddress" value="开封"><br>
<input type="submit" value="提交">
</form>
注意,表单name的值要和实体类的属性名相同,以一对应。
如果想在mvc中用servlet的东西,例如request;
@RequestMapping("welcome2")
public String welcome2(HttpServletRequest request) {
String name=request.getParameter("name");
System.out.println(name);
return"success";
}