Struts2中的ModelDriven机制及其运用

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

package com.carson.demo.model;

import java.io.Serializable;

public class User implements Serializable {

	private String username;
	
	private String password;

	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;
	}
}

假如要写一个Action,用来添加User。

第一种做法是直接在Action中定义所有需要的属性,然后在JSP中直接用属性名称来提交数据:

UserAction:

public class UserAction {

    private String username;
    private String password;
 
    public String add(){
      
       User user = new User();
       user.setUsername(username);
       user.setPassword(password);
       return "success";
    }
   
    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;
    }
   
}

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>登录界面</title>  
    </head>  
      
    <body> 
<form action="login" method="post"> 
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" /> </td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password" /> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录" />
<input type="reset" value="重置" /></td>
</tr>
</table>
</form>
    </body>  
</html>  

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

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

UserAction:

public class UserAction {
    private User user;
    public User getUser() {
       return user;
    }
     public void setUser(User user) {
       this.user = user;
    }
    
}

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>登录界面</title>  
    </head>  
      
    <body> 
<form action="login" method="post"> 
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user.username" /> </td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="user.password" /> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录" />
<input type="reset" value="重置" /></td>
</tr>
</table>
</form>
    </body>  
</html>

这种做法不好的地方是: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 User getUser() {
       return user;
    }
    public void setUser(User user) {
       this.user = user;
    }
}

<%@ page language="java" contentType="text/html; charset=UTF-8"  
    pageEncoding="UTF-8"%>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>登录界面</title>  
    </head>  
      
    <body> 
<form action="login" method="post"> 
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" /> </td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password" /> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录" />
<input type="reset" value="重置" /></td>
</tr>
</table>
</form>
    </body>  
</html> 
可见,第三种做法是比较好的,ActionJSP写起来都比较简单。
博文参考:http://blog.csdn.net/li_tengfei/article/details/6098145

你可能感兴趣的:(java,struts2,modelDriven)