Struts2、Hibernate、Spring整合的泛型DAO,以及通用的分页技术

1.基本DAO泛型接口

[java]  view plain copy
  1. package abu.csdn.dao;  
  2. import java.io.Serializable;  
  3. import java.util.Collection;  
  4. import java.util.List;  
  5. /** 
  6.  * <p/> 
  7.  * 使用泛型作为DAO的通用接口 这里没有提供按名称精确查找,和模糊查找 上述两个方法应该由各自的具体接口去定义 
  8.  * <p/> 
  9.  * 
  10.  * @author Abu 
  11.  * @param <T> : 
  12.  * 持久化的实体Bean 
  13.  * @param <ID> : 
  14.  * 实体Bean的id 
  15.  */  
  16. public interface GenericDao<T, ID extends Serializable> {  
  17.     /** 
  18.      * 保存实体 
  19.      * 
  20.      * @param entity : 
  21.      *               实体 
  22.      * @return 保存后得到的id 
  23.      */  
  24.     public ID save(T entity);  
  25.     /** 
  26.      * 在查找所有记录的时候,使用提供查询语句,查询匹配的记录,否则将使用默认的查询语句查询数据的所有记录. 
  27.      * 
  28.      * @param hql : 自定义的HQL语句 
  29.      */  
  30.     public void setHql(String hql);  
  31.     /** 
  32.      *  
  33.      * @return 自定义的HQL语句 
  34.      */  
  35.     public String getHql();  
  36.     /** 
  37.      * <p> 
  38.      * 删除实体 
  39.      * </p> 
  40.      * 
  41.      * @param entity : 
  42.      *               实体 
  43.      */  
  44.     public void remove(T entity);  
  45.     /** 
  46.      * <p> 
  47.      * 删除实体集合 
  48.      * </p> 
  49.      * 
  50.      * @param entities : 
  51.      *                 实体 
  52.      */  
  53.     public void removeAll(Collection<T> entities);  
  54.     /** 
  55.      * <p> 
  56.      * 修改实体 
  57.      * </p> 
  58.      * 
  59.      * @param entity : 
  60.      *               实体 
  61.      */  
  62.     public void modify(T entity);  
  63.     /** 
  64.      * <p> 
  65.      * 通过名字查找 
  66.      * </p> 
  67.      * 
  68.      * @param id : 
  69.      *           id 
  70.      * @return 找到的实体 
  71.      */  
  72.     public T findById(ID id);  
  73.     /** 
  74.      * <p/> 
  75.      * 查找全部实体 
  76.      * <p/> 
  77.      * 
  78.      * @return 所有实体的列表 
  79.      */  
  80.     public List<T> findAll();  
  81.     /** 
  82.      * <p> 
  83.      * 计算匹配查询条件的记录总数,如果没有注入或者设置hql语句,将使用默认的查询语句返回数据库中所有记录 
  84.      * </p> 
  85.      * 
  86.      * @return 记录总数 
  87.      */  
  88.     public int getTotalRows();  
  89.     /** 
  90.      * <p> 
  91.      * 根据每页记录的数量,计算出总的分页数 
  92.      * </p> 
  93.      * 
  94.      * @param size 每页记录的数量 
  95.      * @return 分页总数 
  96.      */  
  97.     public int getPageSize(int size);  
  98.     /** 
  99.      * <p/> 
  100.      * 根据给定的页码进行分页查找,这是纯Hibernate分页. 
  101.      * <p/> 
  102.      * 
  103.      * @param page : 要查询的页码 
  104.      *             查询的hql语句 
  105.      * @param size : 每页记录数 
  106.      *             分页信息,参见PageInfo 
  107.      * @return 匹配的实体列表 
  108.      */  
  109.     public List<T> findByPage(final int page, final int size);  
  110. }  
 

 

2.基本DAO泛型接口的实现

[java]  view plain copy
  1. package abu.csdn.dao.impl;  
  2. import java.io.Serializable;  
  3. import java.sql.SQLException;  
  4. import java.util.Collection;  
  5. import java.util.List;  
  6. import org.hibernate.HibernateException;  
  7. import org.hibernate.Query;  
  8. import org.hibernate.Session;  
  9. import org.springframework.orm.hibernate3.HibernateCallback;  
  10. import org.springframework.orm.hibernate3.HibernateTemplate;  
  11. import abu.csdn.dao.GenericDao;  
  12. /** 
  13.  *  
  14.  * @author Abu 
  15.  * 
  16.  * @param <T> 
  17.  * @param <ID> 
  18.  */  
  19. public class GenericDaoImpl<T, ID extends Serializable> implements  
  20.         GenericDao<T, ID> {  
  21.     // 具体的实体类型  
  22.     private Class<T> type;  
  23.     // Spring提供的Hibernate工具类  
  24.     private HibernateTemplate hibernateTemplate;  
  25.     // 查询条件  
  26.     private String hql;  
  27.     /** 
  28.      * <p> 
  29.      * 必须提供的构造方法,以便创建实例的时候就知道具体实体的类型 
  30.      * <p> 
  31.      *  
  32.      * @param type : 
  33.      *            实体类型 
  34.      */  
  35.     public GenericDaoImpl(Class<T> type) {  
  36.         this.type = type;  
  37.         this.hql = "from " + type.getName();  
  38.     }  
  39.     /** 
  40.      * <p> 
  41.      * 因为这个类没有继承HibernateDaoSupport,所以现在由Spring注入HibernateTemplate 
  42.      * </p> 
  43.      *  
  44.      * @param hibernateTemplate : 
  45.      *            Spring提供的Hibernate工具类 
  46.      */  
  47.     public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {  
  48.         this.hibernateTemplate = hibernateTemplate;  
  49.     }  
  50.     public void setHql(String hql) {  
  51.         this.hql = hql;  
  52.     }  
  53.     public HibernateTemplate getHibernateTemplate() {  
  54.         return hibernateTemplate;  
  55.     }  
  56.     public String getHql() {  
  57.         return hql;  
  58.     }  
  59.     @SuppressWarnings("unchecked")  
  60.     public List<T> findAll() {  
  61.         String hql = "from " + type.getName();  
  62.         return (List<T>) hibernateTemplate.find(hql);  
  63.     }  
  64.       
  65.     @SuppressWarnings("unchecked")  
  66.     public T findById(ID id) {  
  67.         return (T) hibernateTemplate.get(type, id);  
  68.     }  
  69.     public void modify(T entity) {  
  70.         hibernateTemplate.update(entity);  
  71.     }  
  72.     public void remove(T entity) {  
  73.         hibernateTemplate.delete(entity);  
  74.     }  
  75.       
  76.     public void removeAll(Collection<T> entities) {  
  77.         hibernateTemplate.deleteAll(entities);        
  78.     }  
  79.     @SuppressWarnings("unchecked")  
  80.     public ID save(T entity) {  
  81.         return (ID) hibernateTemplate.save(entity);  
  82.     }  
  83.     public int getTotalRows() {  
  84.         String actualHql = "select count(*) "  
  85.                 + hql.substring(hql.indexOf("from"));  
  86.         return ((Long) this.hibernateTemplate.find(actualHql).get(0))  
  87.                 .intValue();  
  88.     }  
  89.     public int getPageSize(int size) {  
  90.         // 最大页数  
  91.         int pageSize;  
  92.         // 实际每页数据条数  
  93.         int actualSize;  
  94.         // 总记录数  
  95.         int totalRows = this.getTotalRows();  
  96.         // 计算实际每页的条数,如果请求的每页数据条数大于总条数, 则等于总条数  
  97.         actualSize = (size > totalRows) ? totalRows : size;  
  98.         if (totalRows > 0) {  
  99.             pageSize = (totalRows % size == 0) ? (totalRows / actualSize)  
  100.                     : (totalRows / actualSize + 1);  
  101.         } else {  
  102.             pageSize = 0;  
  103.         }  
  104.         return pageSize;  
  105.     }  
  106.     @SuppressWarnings("unchecked")  
  107.     public List<T> findByPage(final int page, final int size) {  
  108.         final int pageSize = this.getPageSize(size);  
  109.         final int totalRows = this.getTotalRows();  
  110.         return hibernateTemplate.executeFind(new HibernateCallback() {  
  111.             public List<T> doInHibernate(Session session)  
  112.                     throws HibernateException, SQLException {  
  113.                 // 实际页码  
  114.                 int actualPage = (page > pageSize) ? pageSize : page;  
  115.                 // 计算实际每页的条数,如果请求的每页数据条数大于总条数, 则等于总条数  
  116.                 int actualSize = (size > totalRows) ? totalRows : size;  
  117.                 // 计算请求页码的第一条记录的索引值,如果  
  118.                 int startRow = (actualPage > 0) ? (actualPage - 1) * actualSize  
  119.                         : 0;  
  120.                 Query query = session.createQuery(hql);  
  121.                 // 设置第一条记录  
  122.                 query.setFirstResult(startRow);  
  123.                 query.setMaxResults(actualSize);  
  124.                 return (List<T>) query.list();  
  125.             }  
  126.         });  
  127.     }  
  128. }  
 

 

3.具体的DAO接口

[java]  view plain copy
  1. package abu.csdn.dao;  
  2. import abu.csdn.bean.User;  
  3. /** 
  4.  *  
  5.  * @author Abu 
  6.  * 
  7.  */  
  8. public interface UserDao extends GenericDao<User, Long> {  
  9.     /** 
  10.      * <p> 
  11.      * 根据用户名精确查找 
  12.      * </p> 
  13.      * @param uname : 用户名 
  14.      * @return : 匹配的实体 
  15.      */  
  16.     public User findByNameExact(String uname);  
  17. }  
 

 

4.具体DAO接口的实现

[java]  view plain copy
  1. package abu.csdn.dao.impl;  
  2. import java.util.List;  
  3. import abu.csdn.bean.User;  
  4. import abu.csdn.dao.UserDao;  
  5. /** 
  6.  *  
  7.  * @author Abu 
  8.  * 
  9.  */  
  10. public class UserDaoImpl extends GenericDaoImpl<User, Long> implements UserDao {  
  11.     public UserDaoImpl(Class<User> type) {  
  12.         super(type);  
  13.     }  
  14.     @SuppressWarnings("unchecked")  
  15.     public User findByNameExact(String uname) {  
  16.         List<User> list = (List<User>) this.getHibernateTemplate().find(  
  17.                 "from User u where u.uname = ?", uname).get(0);  
  18.         return (!list.isEmpty() && list.size() == 1) ? null : list.get(0);         
  19.     }  
  20. }  
 

 

5.基本泛型服务接口

[java]  view plain copy
  1. package abu.csdn.service;  
  2. import java.io.Serializable;  
  3. import java.util.Collection;  
  4. import java.util.List;  
  5. /** 
  6.  * <p> 
  7.  * 基本上与泛型DAO的通用接口一致,请参见GenericDao 
  8.  * <p> 
  9.  *  
  10.  * @author Abu 
  11.  *  
  12.  * @param <T> : 
  13.  *            持久化的实体Bean 
  14.  * @param <ID> : 
  15.  *            实体Bean的id 
  16.  */  
  17. public interface GenericService<T, ID extends Serializable> {  
  18.     /** 
  19.      * 保存实体 
  20.      *  
  21.      * @param entity : 
  22.      *            实体 
  23.      * @return 保存后得到的id 
  24.      */  
  25.     public ID save(T entity);  
  26.     /** 
  27.      * <p> 
  28.      * 删除实体 
  29.      * </p> 
  30.      *  
  31.      * @param entity : 
  32.      *            实体 
  33.      */  
  34.     public void remove(T entity);  
  35.       
  36.       
  37.     /** 
  38.      * <p> 
  39.      * 删除实体集合 
  40.      * </p> 
  41.      *  
  42.      * @param entities : 
  43.      *            实体 
  44.      */  
  45.     public void removeAll(Collection<T> entities);  
  46.     /** 
  47.      * <p> 
  48.      * 修改实体 
  49.      * </p> 
  50.      *  
  51.      * @param entity : 
  52.      *            实体 
  53.      */  
  54.     public void modify(T entity);  
  55.     /** 
  56.      * <p> 
  57.      * 通过名字查找 
  58.      * </p> 
  59.      *  
  60.      * @param id : 
  61.      *            id 
  62.      * @return 找到的实体 
  63.      */  
  64.     public T findById(ID id);  
  65.     /** 
  66.      * <p> 
  67.      * 查找全部实体 
  68.      * <p> 
  69.      *  
  70.      * @return 所有实体的列表 
  71.      */  
  72.     public List<T> findAll();  
  73.     /** 
  74.      * <p> 
  75.      * 根据给定的hql语句进行分页查找 
  76.      * <p> 
  77.      *  
  78.      * @param page : 
  79.      *            要查询的页码 
  80.      * @param size : 
  81.      *            每页记录条数 
  82.      * @return 匹配的实体列表 
  83.      */  
  84.     public List<T> findByPage(final int page, final int size);  
  85.     /** 
  86.      * <p> 
  87.      * 计算匹配查询条件的记录总数,如果没有注入或者设置hql语句,将使用默认的查询语句返回数据库中所有记录 
  88.      * </p> 
  89.      *  
  90.      * @return 记录总数 
  91.      */  
  92.     public int getTotalRows();  
  93.     /** 
  94.      * <p> 
  95.      * 根据每页记录的数量,计算出总的分页数 
  96.      * </p> 
  97.      *  
  98.      * @param size 
  99.      *            每页记录的数量 
  100.      * @return 分页总数 
  101.      */  
  102.     public int getPageSize(int size);  
  103. }  
 

 

6.基本泛型服务接口的实现

[java]  view plain copy
  1. package abu.csdn.service.impl;  
  2. import java.io.Serializable;  
  3. import java.util.Collection;  
  4. import java.util.List;  
  5. import abu.csdn.dao.GenericDao;  
  6. import abu.csdn.service.GenericService;  
  7. /** 
  8.  *  
  9.  * @author Abu 
  10.  * 
  11.  * @param <T> 
  12.  * @param <ID> 
  13.  */  
  14. public class GenericServiceImpl<T, ID extends Serializable> implements  
  15.         GenericService<T, ID> {  
  16.     private GenericDao<T,ID> genericDao;  
  17.       
  18.     public List<T> findAll() {          
  19.         return genericDao.findAll();  
  20.     }  
  21.     public T findById(ID id) {        
  22.         return genericDao.findById(id);  
  23.     }  
  24.     public List<T> findByPage(int page, int size) {         
  25.         return genericDao.findByPage(page, size);  
  26.     }  
  27.     public int getPageSize(int size) {        
  28.         return genericDao.getPageSize(size);  
  29.     }  
  30.     public int getTotalRows() {       
  31.         return genericDao.getTotalRows();  
  32.     }  
  33.     public void modify(T entity) {  
  34.         genericDao.modify(entity);        
  35.     }  
  36.     public void remove(T entity) {  
  37.         genericDao.remove(entity);  
  38.     }  
  39.     public void removeAll(Collection<T> entities) {  
  40.         genericDao.removeAll(entities);       
  41.     }  
  42.       
  43.     public ID save(T entity) {        
  44.         return genericDao.save(entity);  
  45.     }  
  46.     public void setGenericDao(GenericDao<T, ID> genericDao) {  
  47.         this.genericDao = genericDao;  
  48.     }     
  49. }  
 

 

7.具体的服务接口

[java]  view plain copy
  1. package abu.csdn.service;  
  2. import abu.csdn.bean.User;  
  3. /** 
  4.  *  
  5.  * @author Abu 
  6.  * 
  7.  */  
  8. public interface UserService extends GenericService<User, Long> {  
  9.     /** 
  10.      * <p> 
  11.      * 用户登录验证, 登录成功将返回该用户实体,失败则为空 
  12.      * </p> 
  13.      *  
  14.      * @param uname : 
  15.      *            用户名 
  16.      * @param upass : 
  17.      *            密码 
  18.      * @return 用户实体 
  19.      */  
  20.     public User login(String uname, String upass);  
  21.       
  22.     public void removeByUids(long [] uids);  
  23. }  
 

 

8.具体的服务接口的实现

[java]  view plain copy
  1. package abu.csdn.service.impl;  
  2. import java.util.Collection;  
  3. import java.util.List;  
  4. import abu.csdn.bean.User;  
  5. import abu.csdn.dao.UserDao;  
  6. import abu.csdn.dao.impl.UserDaoImpl;  
  7. import abu.csdn.service.UserService;  
  8. /** 
  9.  *  
  10.  * @author Abu 
  11.  * 
  12.  */  
  13. public class UserServiceImpl implements UserService {  
  14.     private UserDao userDao;  
  15.     public void setUserDao(UserDao userDao) {  
  16.         this.userDao = userDao;  
  17.     }  
  18.     public UserDao getUserDao() {  
  19.         return this.userDao;  
  20.     }  
  21.     public List<User> findAll() {  
  22.         return userDao.findAll();  
  23.     }  
  24.     public User findById(Long id) {  
  25.         return userDao.findById(id);  
  26.     }  
  27.     public List<User> findByPage(int page, int size) {  
  28.         return userDao.findByPage(page, size);  
  29.     }  
  30.     public int getPageSize(int size) {  
  31.         return userDao.getPageSize(size);  
  32.     }  
  33.     public int getTotalRows() {  
  34.         return userDao.getTotalRows();  
  35.     }  
  36.     public void modify(User entity) {  
  37.         userDao.modify(entity);  
  38.     }  
  39.     public void removeAll(Collection<User> entities) {  
  40.         userDao.removeAll(entities);  
  41.     }  
  42.     public void remove(User entity) {  
  43.         userDao.remove(entity);  
  44.     }  
  45.     public Long save(User entity) {  
  46.         return userDao.save(entity);  
  47.     }  
  48.     @SuppressWarnings("unchecked")  
  49.     public User login(String uname, String upass) {  
  50.         String hql = "from User u where u.uname = '" + uname + "'"  
  51.                 + " and u.upass = '" + upass + "'";  
  52.         userDao.setHql(hql);  
  53.         List<User> list = userDao.findAll();  
  54.         User user =  (list.isEmpty() && list.size() != 1) ? null : list.get(0);  
  55.         // 千万要记住,一旦修改了默认值,则业务结束后要重新改回默认值   
  56.         userDao.setHql(" from " + User.class.getName());  
  57.           
  58.         return  user;  
  59.     }  
  60.     public void removeByUids(long[] uids) {  
  61.         StringBuffer hqlSb = new StringBuffer();  
  62.         hqlSb.append(" from User u where u.uid in (");  
  63.         for (int i = 0; i < uids.length; i++) {  
  64.             if (i != uids.length - 1) {  
  65.                 hqlSb.append(uids[i]).append(",");  
  66.             } else {  
  67.                 hqlSb.append(uids[i]);  
  68.             }  
  69.         }  
  70.         hqlSb.append(")");  
  71.         userDao.setHql(hqlSb.toString());  
  72.         Collection<User> entities = userDao.findAll();  
  73.         if (entities != null) {  
  74.             userDao.removeAll(entities);  
  75.         }  
  76.     }  
  77. }  
 

 

9.分页数据信息类

[java]  view plain copy
  1. package abu.csdn.util;  
  2. /** 
  3.  * 这个分页信息工具类,只用来保存从HibernateDao中取得的分页信息 
  4.  * @author Abu 
  5.  * 
  6.  */  
  7. public class PageData {  
  8.     // 总记录数  
  9.     private int totalRows;  
  10.     // 每页记录数  
  11.     private int size;  
  12.     // 当前页  
  13.     private int page;  
  14.     // 页面总数  
  15.     private int pageSize;  
  16.     public int getTotalRows() {  
  17.         return totalRows;  
  18.     }  
  19.     public void setTotalRows(int totalRows) {  
  20.         this.totalRows = totalRows;  
  21.     }  
  22.     public int getSize() {  
  23.         return size;  
  24.     }  
  25.     public void setSize(int size) {  
  26.         this.size = size;  
  27.     }  
  28.     public int getPage() {  
  29.         return page;  
  30.     }  
  31.     public void setPage(int page) {  
  32.         this.page = page;  
  33.     }  
  34.     public int getPageSize() {  
  35.         return pageSize;  
  36.     }  
  37.     public void setPageSize(int pageSize) {  
  38.         this.pageSize = pageSize;  
  39.     }  
  40. }  
 

 

10.分页工具类,用来生成客户端的分页代码

[java]  view plain copy
  1. package abu.csdn.util;  
  2. /** 
  3.  * <p> 
  4.  * 生成分页代码,传给目标页面 
  5.  * </p> 
  6.  *  
  7.  * @author Abu 
  8.  *  
  9.  */  
  10. public class PageUtil {  
  11.     // 显示列表显示的页数,默认为5  
  12.     private final int listSize = 4;  
  13.     // 当前页面位于列表的位置  
  14.     private final int halfListSize = 2;  
  15.     // 分页信息  
  16.     private PageData pageData;  
  17.     // 页面url  
  18.     private String url;  
  19.     // 生成的hmtl  
  20.     private String html;  
  21.     /** 
  22.      * <p> 
  23.      * 默认构造方法,提供给Spring注入使用,建议使用带参数的构造方法 
  24.      * </p> 
  25.      */  
  26.     public PageUtil() {  
  27.     }  
  28.     /** 
  29.      * <p> 
  30.      * 构造方法,必须提供分页信息和页面URL 
  31.      *  
  32.      * @param pageInfo : 
  33.      *            分页信息 
  34.      * @param url : 
  35.      *            页面URL,像这样: 
  36.      *  
  37.      * <pre> 
  38.      * /user/userList.do?page= 
  39.      * </pre> 
  40.      */  
  41.     public PageUtil(PageData pageInfo, String url) {  
  42.         this.pageData = pageInfo;  
  43.         this.url = url;  
  44.     }  
  45.     public void setPageData(PageData pageData) {  
  46.         this.pageData = pageData;  
  47.     }  
  48.     public void setUrl(String url) {  
  49.         this.url = url;  
  50.     }  
  51.     /** 
  52.      * 根据分页信息生成分页代码 
  53.      *  
  54.      * @return 分页代码 
  55.      */  
  56.     public String getHtml() {  
  57.         StringBuffer htmlSb = new StringBuffer();  
  58.         // 分页样式单  
  59.         String pageStyle = "<mce:style><!--  
  60.  .ptbl {border:1px solid #CEDBEF;font-size:12px;padding:0;font-family:Arial;width:auto} .ptbl a {text-decoration:none;color:#555555} .ptbl td {padding-top:0px;padding-bottom:0px;padding-left:4px;padding-right:4px}    .strong {background:#CEDBEF;font-weight:800;color:#FF7D00}    .strong a{color:#FF7D00} .page_input {background:#ffffff;border:1px solid #CEDBEF;border-top:none;border-bottom:none;color:#FF7D00;width:30px;margin:0px }   
  61. --></mce:style><style mce_bogus="1"> .ptbl {border:1px solid #CEDBEF;font-size:12px;padding:0;font-family:Arial;width:auto} .ptbl a {text-decoration:none;color:#555555} .ptbl td {padding-top:0px;padding-bottom:0px;padding-left:4px;padding-right:4px}    .strong {background:#CEDBEF;font-weight:800;color:#FF7D00}    .strong a{color:#FF7D00} .page_input {background:#ffffff;border:1px solid #CEDBEF;border-top:none;border-bottom:none;color:#FF7D00;width:30px;margin:0px } </style>";  
  62.         // 表格样式单  
  63.         String pageTable = "<table border='0'cellpadding='0' cellspacing='0' bgcolor='#CEDBEF' class='ptbl'><tr align='center' bgcolor='#FFFBFF'>";  
  64.         // 当前页  
  65.         int page = pageData.getPage();  
  66.         // 总页数  
  67.         int pageSize = pageData.getPageSize();  
  68.         // 上一页  
  69.         int prePage = page - 1;  
  70.         // 判断是否到达了首页,如果是则等于首页  
  71.         prePage = prePage <= 0 ? 1 : prePage;  
  72.         // 下一页  
  73.         int nextPage = page + 1;  
  74.         // 判断是否到达了末页,如果是则等于末页  
  75.         nextPage = nextPage >= pageSize ? pageSize : nextPage;  
  76.         /** 
  77.          * 以下开始正式生成页面代码 
  78.          */  
  79.         // 首先加入样式单  
  80.         htmlSb.append(pageStyle).append(pageTable);  
  81.         // 显示记录总数  
  82.         htmlSb.append("<td>共<font color='red'>" + pageData.getTotalRows()  
  83.                 + "</font>条</td>");  
  84.         if (page != prePage) {  
  85.             // 首页  
  86.             htmlSb.append("<td><a href="" + url + "1" mce_href="" + url + "1">首页</a></td>");  
  87.             // 上一页  
  88.             htmlSb.append("<td><a href="" + url + prePage + "" mce_href="" + url + prePage + "">上一页</a></td>");  
  89.         }  
  90.         // 控制输出列表,仅显示规定的条数  
  91.         if (pageSize <= listSize) {  
  92.             for (int i = 1; i <= pageSize; i++) {  
  93.                 if (i == page)  
  94.                     htmlSb.append("<td class='strong'>" + i + "</td>");  
  95.                 else {  
  96.                     // <td><a href="/user/userList.do?page=1" mce_href="user/userList.do?page=1">1</a></td>  
  97.                     htmlSb.append("<td><a href="" + url + i + "" mce_href="" + url + i + "">" + i  
  98.                             + "</a></td>");  
  99.                 }  
  100.             }  
  101.         } else {  
  102.             int begin = page - halfListSize;  
  103.             begin = (begin <= 0) ? 1 : begin;  
  104.             begin = ((halfListSize + page) > pageSize) ? (pageSize - listSize + 1)  
  105.                     : begin;  
  106.             for (int i = begin; i <= listSize + begin - 1; i++) {  
  107.                 if (i == page) {  
  108.                     htmlSb.append("<td class='strong'>" + i + "</td>");  
  109.                     htmlSb.append("/n");  
  110.                 } else {  
  111.                     htmlSb.append("<td><a href="" + url + i + "" mce_href="" + url + i + "">" + i  
  112.                             + "</a></td>");  
  113.                     htmlSb.append("/n");  
  114.                 }  
  115.             }  
  116.         }  
  117.         // 下一页  
  118.         if (page != pageSize) {  
  119.             htmlSb.append("<td><a href="" + url + nextPage + "" mce_href="" + url + nextPage + "">下一页</a></td>");  
  120.             // 末页  
  121.             htmlSb.append("<td><a href="" + url + pageSize + "" mce_href="" + url + pageSize + "">末页</a></td>");  
  122.         }  
  123.         htmlSb.append("<td>共<font color='red'>" + pageSize  
  124.                 + "</font>页</td></tr></table>");  
  125.         return htmlSb.toString();  
  126.     }  
  127. }  
 

 

11.分页效果图

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值