public class Person {
private String name;
private String city;
private String hobby;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getHobby() {
return hobby;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
<body>
<!-- 使用BeanUtils将form参数封装到 JavaBean Person对象中 -->
<!-- "/itcast"为封装数据的Servlet的url -->
<form action="/itcast" method="post">
姓名 <input type="text" name="name" /> <br/>
城市 <input type="text" name="city" /> <br/>
爱好 <input type="text" name="hobby" /> <br/>
年龄 <input type="text" name="age" /><br/>
<input type="submit" value="提交" />
</form>
</body>
// 将form参数 自动封装 Person对象
Person person = new Person();
try {
BeanUtils.populate(person, request.getParameterMap());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |