struts2_笔记01_请求参数的封装
struts2请求参数的封装
1 直接赋值给简单数据类型
<!-- 属性驱动的方式 -->
<form action="${ pageContext.request.contextPath }/regist1.action" method="post">
姓名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
年龄:<input type="password" name="age" /><br/>
<input type="submit" value="注册" />
</form>
public class Regist1Action extends ActionSupport{
private static final long serialVersionUID = -966487869258031548L;
private String username;
private String password;
private Integer age;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setAge(Integer age) {
this.age = age;
}
public String execute() throws Exception {
System.out.println(username+" "+password+" "+age);
return NONE;
}
}
2 赋值给一个javabean对象
<form action="${pageContext.request.contextPath }/data/user_register.action" method="post">
用户名: <input type="text" name="user.name"/><br/>
密码: <input type="password" name="user.password"/><br/>
性别: <input type="radio" name="user.gender" value="男"/>男
<input type="radio" name="user.gender" value="女"/>女<br/>
爱好:
<input type="checkbox" name="user.hobit" value="篮球"/>篮球
<input type="checkbox" name="user.hobit" value="足球"/>足球
<input type="checkbox" name="user.hobit" value="羽毛球"/>羽毛球<br/>
<input type="submit" value="注册"/>
</form>
public class UserAction2 extends ActionSupport{
//使用一个javabean对象接收
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
注意:请求参数的封装通过struts2的ParametersInterceptor拦截器进行赋值.
3 模型驱动的方式
<!--模型驱动方式-->
<form action="${ pageContext.request.contextPath }/regist3.action" method="post">
姓名:<input type="text" name="username" /><br/>
密码:<input type="password" name="password" /><br/>
年龄:<input type="password" name="age" /><br/>
<input type="submit" value="注册" />
</form>
public class Regist3Action extends ActionSupport implements ModelDriven<User>{
private static final long serialVersionUID = 6556880331550390473L;
// 必须要手动实例化
private User user = new User();
// 获取模型对象
public User getModel() {
return user;
}
public String execute() throws Exception {
System.out.println(user);
return NONE;
}
}
注意:
- 必须实现ModelDriven接口
- 必须要手动实例化对象(需要自己new好)
4 属性驱动的方式,把数据封装到List集合中
<!--向List集合封装数据(默认情况下,采用是属性驱动的方式)-->
<!-- 后台:List<User> list -->
<form action="${ pageContext.request.contextPath }/regist4.action" method="post">
姓名:<input type="text" name="list[0].username" /><br/>
密码:<input type="password" name="list[0].password" /><br/>
年龄:<input type="password" name="list[0].age" /><br/>
姓名:<input type="text" name="list[1].username" /><br/>
密码:<input type="password" name="list[1].password" /><br/>
年龄:<input type="password" name="list[1].age" /><br/>
<input type="submit" value="注册" />
</form>
public class Regist4Action extends ActionSupport{
private static final long serialVersionUID = 6556880331550390473L;
private List<User> list;
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
public String execute() throws Exception {
for (User user : list) {
System.out.println(user);
}
return NONE;
}
}
5 属性驱动的方式,把数据封装到map集合中
<!--向Map集合封装数据(默认情况下,采用是属性驱动的方式)-->
<form action="${ pageContext.request.contextPath }/regist5.action" method="post">
姓名:<input type="text" name="map['one'].username" /><br/>
密码:<input type="password" name="map['one'].password" /><br/>
年龄:<input type="password" name="map['one'].age" /><br/>
姓名:<input type="text" name="map['two'].username" /><br/>
密码:<input type="password" name="map['two'].password" /><br/>
年龄:<input type="password" name="map['two'].age" /><br/>
<input type="submit" value="注册" />
</form>
public class Regist5Action extends ActionSupport{
private static final long serialVersionUID = 6556880331550390473L;
private Map<String, User> map;
public Map<String, User> getMap() {
return map;
}
public void setMap(Map<String, User> map) {
this.map = map;
}
public String execute() throws Exception {
System.out.println(map);
return NONE;
}
}