Struts2实现ModelDriven接口

为什么需要ModelDriven

所谓ModelDriven,意思是直接把实体类当成页面数据的收集对象。比如,有实体类User如下:

package cn.com.leadfar.struts2.actions;

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;

}

publicString getPassword() {

return password;

}

publicvoidsetPassword(String password) {

this.password = password;

}

publicintgetAge() {

return age;

}

publicvoidsetAge(intage) {

this.age = age;

}

publicString getAddress() {

return address;

}

publicvoidsetAddress(String address) {

this.address = address;

}

publicintgetId() {

return id;

}

public void setId(intid) {

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 =newUser();

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(intid) {

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(intage) {

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">

username:<input type="text"><br/>

password:<input type="text"><br/>

age:<input type="text"><br/>

address:<input type="text"><br/>

<input type="submit" value="添加用户">

</form><br/>
 

上述做法不好之处是:如果实体类的属性非常多,那么Action中也要定义相同的属性。

第二种做法是将User对象定义到UserAction中,然后在JSP中通过user属性来给user赋值:

UserAction:


public class UserAction {

private User user;

public String add(){

newUserManager().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">

username:<input type="text"><br/>

password:<input type="text"><br/>

age:<input type="text"><br/>

address:<input type="text"><br/>

<input type="submit" value="添加用户">

</form><br/>
 

这种做法不好的地方是:JSP页面上表单域中的命名变得太长

第三种做法是利用ModelDriven机制,让UserAction实现一个ModelDriven接口,同时实现接口中的getModel()。实现了ModelDriven就必须实现getModel这个方法,该方法返回的是你的ModelDrivern泛型中设定的实体类。其实是为了实现代码的重用,不要再定义一次你在数据访问层定义的属性了,直接用那个类就行了.如下所示:


public class UserAction implements ModelDriven{

private User user;

@Override

public Object getModel() {

if(user==null){

user=newUser();

}

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">

username:<input type="text"><br/>

password:<input type="text"><br/>

age:<input type="text"name="age"><br/>

<input type="submit" value="添加用户">

</form><br/>
 

可见,第三种做法是比较好的,Action和JSP写起来都比较简单。

ModelDriven背后的机制?

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/ee6f2fe739f94f3ca588ea7f99eff252.html