模型驱动即ModelDriver,所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象。
比如,有实体类User如下:
public class User { private int id; private String username; private String password; private int age; private String address; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } }
假如要写一个Action,用来添加User。
第一种做法是直接在Action中定义所有需要的属性,然后在JSP中直接用属性名称来提交数据:
UserAction:
public class UserAction { private int id; private String username; private String password; private int age; private String address; public String add(){ User user = new User(); user.setId(id); user.setUsername(username); user.setPassword(password); user.setAge(age); user.setAddress(address); new UserManager().addUser(user); return "success"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
add_input.jsp:
<form action="test/user.action" method="post"> <input type="hidden" name="method:add"> username:<input type="text" name="username"> <br/> password:<input type="text" name="password"> <br/> age:<input type="text" name="age"> <br/> address:<input type="text" name="address"> <br/> <input type="submit" name="submit" value="添加用户"> </form> <br/>
上述做法不好之处是:如果实体类的属性非常多,那么Action中也要定义相同的属性。
第二种做法是将User对象定义到UserAction中,然后在JSP中通过user属性来给user赋值:
UserAction:
public class UserAction { private User user; public String add(){ new UserManager().addUser(user); return "success"; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
add_input.jsp:
<form action="test/user.action" method="post"> <input type="hidden" name="method:add"> username:<input type="text" name="user.username"> <br/> password:<input type="text" name="user.password"> <br/> age:<input type="text" name="user.age"> <br/> address:<input type="text" name="user.address"> <br/> <input type="submit" name="submit" value="添加用户"> </form> <br/>
这种做法不好的地方是:JSP页面上表单域中的命名变得太长
第三种做法是利用ModelDriven机制,让UserAction实现一个ModelDriven接口,同时实现接口中的方法:getModel()。如下所示:
public class UserAction implements ModelDriven{ private User user; @Override public Object getModel() { if(user == null){ user = new User(); } return user; } public String add(){ new UserManager().addUser(user); return "success"; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
JSP的代码如下:
<form action="test/user.action" method="post"> <input type="hidden" name="method:add"> username:<input type="text" name="username"> <br/> password:<input type="text" name="password"> <br/> age:<input type="text" name="age"> <br/> <input type="submit" name="submit" value="添加用户"> </form> <br/>