java web中分層MVC的意義


在web編程中,由於高內聚、低耦合的特點,需要將多個類實現多層,大致有以下幾層:
①entity,實體類,如user,role等,這些類里邊包含了私有屬性和公共的get、set方法這和數據庫中的表相對應,更嚴格一些,包括字段的順序和type都要保持一致。
②base,封裝了基本的操作數據庫的方法(增刪改查)
③dao,訪問並操作數據庫,若想從數據庫獲取數據,必須調用dao層。dao層沒有一點業務邏輯,全是跟數據庫操作有關的類
④service,業務邏輯層,里邊可以是接口。只要沒有訪問數據庫的操作,都寫到service層中。

舉個例子說明什么是業務。如做一個分頁功能,數據1000條,每頁20條,則可以把這個功能寫成工具類封裝起來,然后在業務層調用這個封裝的方法,這才是業務層要做的事。

service層和dao層有什么區別?
再舉個例子,如注冊用戶時,還想往日志表里添加一個日志,那么就要在業務層來實現這個操作,並對該操作添加事務,效果會更好。若不使用service,只用dao的話,那么就要連續使用userDao和logDao,那么萬一某一步出錯,就可能造成user加進去而log沒有添加成功。

⑤action,也就是原來的servlet,位於jsp和service層之間進行交互。主要的操作就是請求(request)和響應(response)。存取某個功能的整體實現方法。

以后不要在servlet中調用dao層,而要寫到serviceImpl(對接口的實現層)中,如以前的userServlet中有
private UserDao udao = new UserDaoImpl();//private必須寫
以后這一句要寫到UserServiceImpl內。

⑥vo,數據傳輸層,用來轉入轉出數據,跟前台有關。
⑦util,工具類,用來封裝一些公用的方法,如字符串的處理,字符集過濾等。

⑧impl,用來實現接口。一個接口可以有多種實現,用來滿足不同需要。

一般的調用情況如下:

 

貼一張很重要的圖,大致說明了各層的繼承和implements關系

 

 

舉一個小項目的例子,實現往數據庫增刪改查用戶。縮略圖如下:

 

其中User.java和UserVO.java現在看起來內容相同,區別在於User類是底層數據,一般不輕易改變。UserVO層業余數據庫中內容相對應,將DB中檢索出數據,或者要往DB中反映的數據保存在vo實例中。以后若想再有什么擴展,直接在VO層改就行,不用動底層代碼。

這里邊,UserDao,BaseDao,UserService都是接口類,相應的impl則是對其的實現。

1.BaseDao代碼:

 

[java]  view plain  copy
 
  1. import java.util.List;  
  2. /** 
  3.  * 基本的方法 
  4.  * @author Administrator 
  5.  * 
  6.  */  
  7. public interface BaseDao<Entity> {  
  8.   
  9.     /** 
  10.      * 保存 
  11.      * @param obj 
  12.      * @throws Exception 
  13.      */  
  14.     void save(Entity entity) throws Exception;  
  15.     /** 
  16.      * 刪除 
  17.      * @param id 
  18.      * @throws Exception 
  19.      */  
  20.     void delete(int id) throws Exception;  
  21.     /** 
  22.      * 更新 
  23.      * @param obj 
  24.      * @throws Exception 
  25.      */  
  26.     void update(Entity entity) throws Exception ;  
  27.     /** 
  28.      * 根據id查詢指定記錄 
  29.      * @param id 
  30.      * @return 
  31.      * @throws Exception 
  32.      */  
  33.     Entity findById(int id) throws Exception;  
  34.     /** 
  35.      * 查詢所有記錄 
  36.      * @return 
  37.      * @throws Exception 
  38.      */  
  39.     List<Entity> findAll() throws Exception;  
  40.       
  41.       
  42. }  

2.BaseDaoImpl代碼,利用反射獲取數據庫數據

 

[java]  view plain  copy
 
  1. import java.lang.reflect.Field;  
  2. import java.lang.reflect.Method;  
  3. import java.lang.reflect.ParameterizedType;  
  4. import java.sql.Connection;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import com.bjsxt.util.DBUtil;  
  11.   
  12. public class BaseDaoImpl<Entity> implements BaseDao<Entity> {  
  13.       
  14.     protected Class clazz;  
  15.       
  16.     public BaseDaoImpl(){  
  17.         ParameterizedType pz = (ParameterizedType) this.getClass().getGenericSuperclass();  
  18.         //利用反射返回當前類的父類的類型,並強制轉換  
  19.         clazz = (Class) pz.getActualTypeArguments()[0];  
  20.         //返回此類型實際類型參數的Type對象數組的第一個  
  21.         //這兩步的作用就是獲取父類泛型參數的實際類型  
  22.         System.out.println(clazz + "這是測試代碼,打印出來的");  
  23.     }  
  24.       
  25.       
  26.     @Override  
  27.     public void save(Entity entity) throws Exception {  
  28.         Connection conn = DBUtil.getConn();//連接數據庫  
  29.         String sql = "insert into " + clazz.getSimpleName() + " values(null";  
  30.         Field[] fs = clazz.getDeclaredFields();   
  31.         for (int i = 0; i < fs.length; i++) {  
  32.             sql += ",?";  
  33.         }  
  34.         sql = sql + ")";  
  35.         PreparedStatement ps = DBUtil.getPs(conn, sql);  
  36.         //user.setUsername(rs.getString("username"));  
  37.         for (int i = 0; i < fs.length; i++) {  
  38.             String methodName = "get" + Character.toUpperCase(fs[i].getName().charAt(0)) + fs[i].getName().substring(1);  
  39.             //假如有個實體user類,含有uname屬性,則獲取uname的方法默認會為getUname(),會將uname的首字母大寫,其余不變  
  40.             //Character.toUpperCase就是將字符大寫,然后將除首字母以外的截取出來,拼到一起,構成類似於getUname()的方法  
  41.             //user.setUsername(uForm.getUsername);    
  42.             //該處用get是因為要把對象保存進數據庫里,用get獲取屬性  
  43.             Method m = clazz.getDeclaredMethod(methodName);  
  44.             ps.setObject(i,m.invoke(entity));  
  45.             //setObject用來給"?"賦值。invoke則是在不知道具體類的情況下利用字符串去調用方法  
  46.             //正常情況下是user.getUsername,m.invoke(entity)中m是一個方法,類似於getUsername(),entity相當於user.  
  47.             //invoke是從實體里邊找方法,那句話的意思就是從實體里邊調用方法  
  48.         }  
  49.         ps.executeUpdate();  
  50.         DBUtil.close(ps);  
  51.         DBUtil.close(conn);  
  52.     }  
  53.   
  54.     @Override  
  55.     public void view(Entity entity) throws Exception {  
  56.         // TODO Auto-generated method stub  
  57.           
  58.     }  
  59.   
  60.     @Override  
  61.     public void update(Entity entity) throws Exception {  
  62.         Connection conn = DBUtil.getConn();  
  63.         String sql = "update " + clazz.getSimpleName() + "set";  
  64.         Field[] fs = clazz.getDeclaredFields();  
  65.         for (int i = 0; i < fs.length; i++) {  
  66.             sql += fs[i].getName() + " = ?,";  
  67.         }  
  68.         sql = sql.substring(0,sql.length()-1) + " where id = ?"; //減去最后的逗號  
  69.         PreparedStatement ps = DBUtil.getPs(conn, sql);  
  70.         for (int i = 0; i < fs.length; i++) {  
  71.             String methodName = "get" + Character.toUpperCase(fs[i].getName().charAt(0)) + fs[i].getName().substring(1);  
  72.             Method m = clazz.getDeclaredMethod(methodName);  
  73.             ps.setObject(i, m.invoke(entity));  
  74.         }  
  75.         Method getId = clazz.getDeclaredMethod("getId");//更新需要知道id  
  76.         ps.setInt(fs.length, (Integer) getId.invoke(entity));//因為id在sql語句最后一個問號處,所以要用fs.length  
  77.         ps.executeUpdate();  
  78.         DBUtil.close(ps);  
  79.         DBUtil.close(conn);  
  80.           
  81.     }  
  82.   
  83.     @Override  
  84.     public void delete(int id) throws Exception {  
  85.         Connection conn = DBUtil.getConn();  
  86.         String sql = "delete from " + clazz.getSimpleName() + " where id = ?" + id;  
  87.         PreparedStatement ps = DBUtil.getPs(conn, sql);  
  88.         ps.executeUpdate();  
  89.         DBUtil.close(ps);  
  90.         DBUtil.close(conn);  
  91.     }  
  92.   
  93.     @Override  
  94.     public Entity findById(int id) throws Exception {  
  95.         Connection conn = DBUtil.getConn();  
  96.         String sql = "select * from " + clazz.getSimpleName() + " where id= ? ";  
  97.         PreparedStatement ps = DBUtil.getPs(conn, sql);  
  98.         ps.setInt(1,id);  
  99.         ResultSet rs = ps.executeQuery();  
  100.         Entity entity = (Entity) clazz.newInstance();//利用無參構造函數新建實例  
  101.         Field[] fs = clazz.getDeclaredFields();  
  102.         if(rs.next()){  
  103.             for (int i = 0; i < fs.length; i++) {  
  104.                 String methodName = "set" + Character.toUpperCase(fs[i].getName().charAt(0)) + fs[i].getName().substring(1);  
  105.                 Method m = clazz.getDeclaredMethod(methodName, fs[i].getType());  
  106.                 m.invoke(entity, rs.getObject(fs[i].getName()));  
  107.             }  
  108.         }  
  109.         DBUtil.close(rs);  
  110.         DBUtil.close(ps);  
  111.         DBUtil.close(conn);  
  112.           
  113.         return entity;  
  114.     }  
  115.   
  116.     @Override  
  117.     public List<Entity> findAll() throws Exception {  
  118.         Connection conn = DBUtil.getConn();  
  119.         String sql = "select * from " + clazz.getSimpleName();  
  120.         PreparedStatement ps = DBUtil.getPs(conn, sql);  
  121.         ResultSet rs = ps.executeQuery();  
  122.         List<Entity> enList = new ArrayList<Entity>();  
  123.         while(rs.next()){  
  124.             Entity en = (Entity) clazz.newInstance();  
  125.             //user.setUsername(rs.getString("username"));  
  126.             Field[] fs = clazz.getDeclaredFields();  
  127.             for (int i = 0; i < fs.length; i++) {  
  128.                 String methodName = "set" + Character.toUpperCase(fs[i].getName().charAt(0)) + fs[i].getName().substring(1);  
  129.                 Method m = clazz.getDeclaredMethod(methodName, fs[i].getType());  
  130.                 m.invoke(en, rs.getObject(fs[i].getName()));  
  131.             }  
  132.             enList.add(en);  
  133.         }  
  134.         DBUtil.close(rs);  
  135.         DBUtil.close(ps);  
  136.         DBUtil.close(conn);  
  137.         return enList;  
  138.     }  
  139.       
  140. }  

注意:String methodName在何處用get,何處用set

3.UserDao代碼

 

 

[java]  view plain  copy
 
  1. import java.util.List;  
  2.   
  3. import com.bjsxt.base.BaseDao;  
  4. import com.bjsxt.entity.User;  
  5.   
  6. public interface UserDao  extends BaseDao<User>{  
  7.   
  8.     List<User> findByPageList(int currentPage, int pageSize) throws Exception;  
  9.   
  10.     Long getTotal() throws Exception;  
  11.   
  12. }  


4.UserDaoImpl代碼

 

 

[java]  view plain  copy
 
  1. import java.sql.Connection;  
  2. import java.sql.PreparedStatement;  
  3. import java.sql.ResultSet;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import sun.nio.cs.US_ASCII;  
  8.   
  9. import com.bjsxt.base.BaseDaoImpl;  
  10. import com.bjsxt.dao.UserDao;  
  11. import com.bjsxt.entity.User;  
  12. import com.bjsxt.util.DBUtils;  
  13.   
  14. public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{  
  15.   
  16.     @Override  
  17.     public List<User> findByPageList(int currentPage, int pageSize)  
  18.             throws Exception {  
  19.         Connection conn = DBUtils.getConn();  
  20.         String sql = "select * from user limit " + (currentPage-1)*pageSize + "," +pageSize;  
  21.         PreparedStatement ps = DBUtils.getPs(conn, sql);  
  22.         ResultSet rs = ps.executeQuery();  
  23.         List<User> userlist =new ArrayList<User>();  
  24.         while(rs.next()){  
  25.             User user = new User();  
  26.             user.setId(rs.getInt("id"));  
  27.             user.setUsername(rs.getString("username"));  
  28.             user.setPassword(rs.getString("password"));  
  29.             user.setAge(rs.getInt("age"));  
  30.             user.setSex(rs.getString("sex"));  
  31.             user.setBirthday(rs.getString("birthday"));  
  32.             user.setSalary(rs.getString("salary"));  
  33.             user.setDescription(rs.getString("description"));  
  34.             userlist.add(user);  
  35.         }  
  36.         DBUtils.close(rs);  
  37.         DBUtils.close(ps);  
  38.         DBUtils.close(conn);  
  39.         return userlist;  
  40.     }  
  41.   
  42.     @Override  
  43.     public Long getTotal() throws Exception {  
  44.         Connection conn = DBUtils.getConn();  
  45.         String sql = "select count(*) from user";  
  46.         PreparedStatement ps = DBUtils.getPs(conn, sql);      
  47.         ResultSet rs =ps.executeQuery();  
  48.         if(rs.next()){  
  49.             return ((Number)rs.getInt(1)).longValue();  
  50.         }  
  51.         return ((Number)0).longValue();  
  52.     }  
  53.   
  54. }  

5.User代碼,下邊的UserVO先和它一樣,就不貼了。

 

 

[java]  view plain  copy
 
  1. public class User {  
  2.   
  3.     private int id ;   
  4.     private String username ;   
  5.     private String password ;   
  6.     private int age ;   
  7.     private String sex ;   
  8.     private String birthday ;   
  9.     private String salary ;   
  10.     private String description ;  
  11.       
  12.       
  13.     public User() {  
  14.         super();  
  15.         // TODO Auto-generated constructor stub  
  16.     }  
  17.     public User(int id, String username, String password, int age, String sex,  
  18.             String birthday, String salary, String description) {  
  19.         super();  
  20.         this.id = id;  
  21.         this.username = username;  
  22.         this.password = password;  
  23.         this.age = age;  
  24.         this.sex = sex;  
  25.         this.birthday = birthday;  
  26.         this.salary = salary;  
  27.         this.description = description;  
  28.     }  
  29.     public int getId() {  
  30.         return id;  
  31.     }  
  32.     public void setId(int id) {  
  33.         this.id = id;  
  34.     }  
  35.     public String getUsername() {  
  36.         return username;  
  37.     }  
  38.     public void setUsername(String username) {  
  39.         this.username = username;  
  40.     }  
  41.     public String getPassword() {  
  42.         return password;  
  43.     }  
  44.     public void setPassword(String password) {  
  45.         this.password = password;  
  46.     }  
  47.     public int getAge() {  
  48.         return age;  
  49.     }  
  50.     public void setAge(int age) {  
  51.         this.age = age;  
  52.     }  
  53.     public String getSex() {  
  54.         return sex;  
  55.     }  
  56.     public void setSex(String sex) {  
  57.         this.sex = sex;  
  58.     }  
  59.     public String getBirthday() {  
  60.         return birthday;  
  61.     }  
  62.     public void setBirthday(String birthday) {  
  63.         this.birthday = birthday;  
  64.     }  
  65.     public String getSalary() {  
  66.         return salary;  
  67.     }  
  68.     public void setSalary(String salary) {  
  69.         this.salary = salary;  
  70.     }  
  71.     public String getDescription() {  
  72.         return description;  
  73.     }  
  74.     public void setDescription(String description) {  
  75.         this.description = description;  
  76.     }   
  77.       
  78. }  

6.UserService代碼,只提供接口,不實現任何方法,只讓UserServiceImpl來實現

 

 

[javascript]  view plain  copy
 
  1. import java.util.List;  
  2.   
  3. import com.bjsxt.util.DataGrid;  
  4. import com.bjsxt.vo.UserVO;  
  5.   
  6. public interface UserService {  
  7.       
  8.     void saveUser(UserVO userVO) throws Exception ;   
  9.       
  10.     void deleteUser(int id) throws Exception ;  
  11.       
  12.     void updateUser(UserVO userVO) throws Exception ;  
  13.       
  14.     UserVO findById(int id) throws Exception ;   
  15.       
  16.     List<UserVO> findAll() throws Exception ;  
  17.       
  18.     DataGrid findByPage(int currentPage , int PageSize) throws Exception ;  
  19.       
  20.       
  21. }  


7.UserServiceImpl代碼,方法還沒完全實現,只完成一種。

 

 

[java]  view plain  copy
 
  1. import java.nio.channels.DatagramChannel;  
  2. import java.util.ArrayList;  
  3. import java.util.Iterator;  
  4. import java.util.List;  
  5.   
  6. import com.bjsxt.dao.UserDao;  
  7. import com.bjsxt.dao.impl.UserDaoImpl;  
  8. import com.bjsxt.entity.User;  
  9. import com.bjsxt.service.UserService;  
  10. import com.bjsxt.util.DataGrid;  
  11. import com.bjsxt.vo.UserVO;  
  12.   
  13. public class UserServiceImpl implements UserService {  
  14.       
  15.       
  16.     private UserDao userDao  = new UserDaoImpl();  
  17.   
  18.     @Override  
  19.     public void saveUser(UserVO userVO) throws Exception {  
  20.           
  21.     }  
  22.   
  23.     @Override  
  24.     public void deleteUser(int id) throws Exception {  
  25.           
  26.     }  
  27.   
  28.     @Override  
  29.     public void updateUser(UserVO userVO) throws Exception {  
  30.           
  31.     }  
  32.   
  33.     @Override  
  34.     public UserVO findById(int id) throws Exception {  
  35.         return null;  
  36.     }  
  37.   
  38.     @Override  
  39.     public List<UserVO> findAll() throws Exception {  
  40.         return null;  
  41.     }  
  42.   
  43.     @Override  
  44.     public DataGrid findByPage(int currentPage, int PageSize)  
  45.             throws Exception {  
  46.         List<UserVO> uservolist = new ArrayList<UserVO>();  
  47.         List<User> userList = this.userDao.findByPageList(currentPage ,PageSize);  
  48.         for (Iterator iterator = userList.iterator(); iterator.hasNext();) {  
  49.             User user = (User) iterator.next();  
  50.             UserVO uvo = new UserVO();  
  51.             uvo.setId(user.getId());  
  52.             uvo.setUsername(user.getUsername());  
  53.             uvo.setPassword(user.getPassword());  
  54.             uvo.setAge(user.getAge());  
  55.             uvo.setBirthday(user.getBirthday());  
  56.             uvo.setSalary(user.getSalary());  
  57.             uvo.setSex(user.getSex());  
  58.             uvo.setDescription(user.getDescription());  
  59.             uservolist.add(uvo);  
  60.         }  
  61.           
  62.         Long total = this.userDao.getTotal();  
  63.         DataGrid datagrid = new DataGrid();  
  64.         datagrid.setRows(uservolist);  
  65.         datagrid.setTotal(total);  
  66.           
  67.         return datagrid;  
  68.     }  
  69.   
  70. }  

8.DataGrid代碼

 

 

[java]  view plain  copy
 
  1. import java.util.List;  
  2.   
  3. public class DataGrid {  
  4.   
  5.     private Long total ;    //總記錄數  
  6.     private List rows  ;    //返回的數據條目  
  7.     public Long getTotal() {  
  8.         return total;  
  9.     }  
  10.     public void setTotal(Long total) {  
  11.         this.total = total;  
  12.     }  
  13.     public List getRows() {  
  14.         return rows;  
  15.     }  
  16.     public void setRows(List rows) {  
  17.         this.rows = rows;  
  18.     }  
  19.       
  20.       
  21. }  


9.DBUtil代碼

 

 

[java]  view plain  copy
 
  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6.   
  7. /** 
  8.  * 鏈接數據庫的 
  9.  * @author Administrator 
  10.  * 
  11.  */  
  12. public class DBUtils {  
  13.   
  14.     /** 
  15.      * 獲得connection對象 
  16.      * @return 
  17.      */  
  18.     public static  Connection getConn(){  
  19.         Connection conn = null;  
  20.         try {  
  21.             Class.forName("com.mysql.jdbc.Driver");  
  22.             conn = DriverManager.getConnection("jdbc:mysql://localhost/ext_001", "root", "root");  
  23.         } catch (ClassNotFoundException e) {  
  24.             e.printStackTrace();  
  25.         } catch (SQLException e) {  
  26.             // TODO Auto-generated catch block  
  27.             e.printStackTrace();  
  28.         }  
  29.         return conn;  
  30.     }  
  31.       
  32.     /** 
  33.      * 預編譯sql 
  34.      * @param conn 
  35.      * @param sql 
  36.      * @return 
  37.      */  
  38.     public static PreparedStatement getPs(Connection conn , String sql){  
  39.         PreparedStatement ps = null;  
  40.         try {  
  41.             ps = conn.prepareStatement(sql);  
  42.         } catch (SQLException e) {  
  43.             e.printStackTrace();  
  44.         }  
  45.         return ps;  
  46.     }  
  47.       
  48.     /** 
  49.      * 一系列關閉對象的close 
  50.      * @param conn 
  51.      */  
  52.       
  53.       
  54.     public static void close(Connection conn){  
  55.         if(conn != null){  
  56.             try {  
  57.                 conn.close();  
  58.             } catch (SQLException e) {  
  59.                 e.printStackTrace();  
  60.             }  
  61.         }  
  62.     }  
  63.       
  64.     public static  void close(PreparedStatement ps){  
  65.         if(ps != null){  
  66.             try {  
  67.                 ps.close();  
  68.             } catch (SQLException e) {  
  69.                 e.printStackTrace();  
  70.             }  
  71.         }         
  72.     }  
  73.       
  74.     public static  void close(ResultSet rs){  
  75.         if(rs != null){  
  76.             try {  
  77.                 rs.close();  
  78.             } catch (SQLException e) {  
  79.                 e.printStackTrace();  
  80.             }  
  81.         }         
  82.     }  
  83. }  

 

最后粘一篇百度知道上的內容,關於service層和dao層的,供參考。

首先,service層會很大,方法很多。 
第二,試想,所有DAO都有增刪改查四個基本方法。假設在DAO層(如StudentDAO.java)里student.add(),student.delete().student.update(),student.query()。
而如果應用增加了service層(如ApplictionServiceImpl.java),需要在service層中加上 
applicationService.addStudent(),deleteStudent(),updateStudent(),queryStudent()四個方法。
這時你需要更新service層的接口類ApplicationService.Java,加上這四個方法。然后再更新service層的實現類ApplicationServiceImpl,加上這四個方法,最后你發現這四個方法每個方法里都只有一句話(以addStudent()為例)
public int addStudent(Student student){ 
    return student.add(); 

這樣是不是太傻了點,還不如在action中直接調用StudentDAO中的student.add()。 
以上是我的個人看法,請各位指點





這么來講好了 按你所說 在action中直接調用StudentDAO中的student.add()。 現在有客戶A 客戶B 

客戶A的需求是 addStudent 這個和你所說的情況一致

客戶B的需求是 addStudent 前去加一個動作 將學生父母的信息也插入數據庫

這時如果按照只調用DAO的方法的話,你需要從新建立一個action 
CustomerBAction 
再重新寫一個DAO 因為這個DAO里要有添加父母的方法 student.addStudentAndParentInfo()。 

CustomerBAction 去調用 student.addStudentAndParentInfo()。 

這樣加大了很多工作量

如果中間有service層的話 action始終用一個action
而調用的也是service接口 只要對接口注入不同的實現就可以滿足 不同客戶的需求了

知識要活學活用,要按照自己項目以后的發展趨勢來搭設環境,別人家說什么就用什么。其實有時候javabean+jsp 也很好用啊,因為jsp不用重啟服務 開發速度很快。我做小項目就是這種模式,以后小項目的更改也會很少,所以不用搭建的過於復雜。

 

補充:

entity和vo的不同還可以通過下邊這個例子說明。如有user.java和userVO.java
user的屬性為

 

[java]  view plain  copy
 
  1. private int id ;   
  2. private String username ;   
  3. private String password ;  

而userVO的屬性則可為:

 

 

[java]  view plain  copy
 
  1. private int id ;   
  2. private String username ;   
  3. private String password1 ;  //第一次輸入密碼  
  4. private String password2 ;  //確認密碼  

在表單驗證時,password1和password2相同時才會被提交。這樣不用改entity類,只需修改vo層即可。當然,這里的password1和password2也得寫上get和set方法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM