1.属性驱动
*提供属性的set方法
HTML:
<h1>数据封装方式一:提供set方法</h1> <form action="${pageContext.request.contextPath}/xxx.action" method="post"> 姓名:<input type="text" name="name" /><br/> 年龄:<input type="text" name="age" /> 提交:<input type="submit" value="提交" /> </form>
Action:
public class xxxAction extends ActionSupport { private String name; private Integer age; public void setName() { this.name = name; } public void setAge() { this.age = age; } public String execute() throws Exception { System.out.println(name); System.out.println(age); return NONE; } }
2.页面表达式的方式
HTML:
<h1>数据封装方式二:提供页面OGNL表达式的方式</h1> <form action="${pageContext.request.contextPath}/xxx.action" method="post"> 姓名:<input type="text" name="user.name" /><br/> 年龄:<input type="text" name="user.age" /><br/> 提交:<input type="submit" value="提交" /> </form>
Action:
public class xxxAction extends ActionSupport { // 在Action中封装对象的属性:提供get AND set方法 private User user; public void setUser() { this.user = user; } public void getUser() { this.user = user; } public String execute() throws Exception { System.out.println(user); return NONE; } }
3.模型驱动(!)
HTML:
<h1>数据封装方式三:模型驱动的方式</h1> <form action="${pageContext.request.contextPath}/xxx.action" method="post"> 姓名:<input type="text" name="name" /><br/> 年龄:<input type="text" name="age" /><br/> 提交:<input type="submit" value="提交" /> </form>
Action:
public class xxxAction extends ActionSupport implements ModelDriven<User> { private User user = new User(); // 模型驱动的使用方法: @Override public User getModel() { return user; } public String execute() throws Exception { System.out.println(user); return NONE; } }