1.采用ActionContext对象的方式
public String execute() throws Exception { // 获取ActionContext对象 ActionContext context = ActionContext.getContext(); // 获取参数Map对象 Map<String, Object> map = context.getParameters(); // 遍历Map集合 Set<String> set = map.keySet(); for (String key : set) { // 表单value值是Object数组,因为存在表单中多值的情况 Object[] values = (Object[]) map.get(key); System.out.println(Arrays.toString(values)); } return NONE; }
2.采用ServletActionContext对象的方式
public String execute() throws Exception { // 通过ServletActionContext对象得到HttpServletRequest对象 HttpServletRequest request = ServletActionContext.getRequest(); // 取值 String username = request.getParameter("username"); String password = request.getParameter("password"); String address = request.getParameter("address"); // 创建User对象并复制进去 return NONE;
3.采用接口注入的方式
public class TestAction3 extends ActionSupport implements ServletRequestAware { private HttpServletRequest request = null; public void setServletRequest(HttpServletRequest request) { // TODO Auto-generated method stub this.request = request; } @Override public String execute() throws Exception { // TODO Auto-generated method stub String username = request.getParameter("username"); String password = request.getParameter("password"); String address = request.getParameter("address"); System.out.println(username + ":" + password + ":" + address); return NONE; }
4.采用属性封装
表单数据
<form action="${pageContext.request.contextPath }/form.action" method="post"> 用户名:<input type="text" name="username"/><br/> 密 码:<input type="password" name="password"/><br/> 地 址:<input type="text" name="address"/><br/> <input type="submit" value="提交"/> </form>
声明表单属性,并生成set方法
private String username; private String password; private String address; public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setAddress(String address) { this.address = address; } public String execute() throws Exception { User user = new User(); user.setUsername(username); user.setPassword(password); user.setAddress(address); return NONE; }
5.模型驱动封装(重点)
public class DemoAction1 extends ActionSupport implements ModelDriven<User> { private User user = new User(); public User getUser() { return user; } @Override public User getModel() { // TODO Auto-generated method stub return user; } public String execute() throws Exception { System.out.println(user); return NONE; } }
6.表达式封装
表单数据,注意name属性值的写法,实体对象。属性名
<form action="${pageContext.request.contextPath }/form.action" method="post"> 用户名:<input type="text" name="user.username"/><br/> 密 码:<input type="password" name="user.password"/><br/> 地 址:<input type="text" name="user.address"/><br/> <input type="submit" value="提交"/> </form>
代码,注意生成实体类的set和get方法
private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String execute() throws Exception { System.out.println(user); return NONE; }
7.List集合封装
表单数据 [0]为list下标值
<form action="${pageContext.request.contextPath }/form5" method="post"> 用户名:<input type="text" name="list[0].username"/><br/> 密 码:<input type="password" name="list[0].password"/><br/> 年 龄:<input type="text" name="list[0].age"/><br/> 地 址:<input type="text" name="list[0].address"/><br/> <input type="submit" value="提交"/> </form>
代码
private List<User> list; public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println(list); return NONE; }
8.Map集合封装
页面 'one'为键
<form action="${pageContext.request.contextPath }/form6" method="post"> 用户名:<input type="text" name="map['one'].username"/><br/> 密 码:<input type="password" name="map['one'].password"/><br/> 年 龄:<input type="text" name="map['one'].age"/><br/> 地 址:<input type="text" name="map['one'].address"/><br/> <input type="submit" value="提交"/> </form>
代码
private Map<String, User> map; public Map<String, User> getMap() { return map; } public void setMap(Map<String, User> map) { this.map = map; } @Override public String execute() throws Exception { // TODO Auto-generated method stub System.out.println(map); return NONE; }