ITeye博客

ssh整合分页

wangyan2009 · 2010-01-24 21:43 · struts2 · 企业架构 · Markdown

1、DAO层接口的设计,在MemberDao接口中定义了如下两个方法:

public interface MemberDao{      //省略了其他的代码      /** //*    * 分页查询    * @param hql 查询的条件    * @param offset 开始记录    * @param length 一次查询几条记录    * @return    /   public List queryForPage(final String hql,final int offset,final int length);      /* //*    * 查询所有记录数    * @param hql 查询的条件    * @return 总记录数    */   public int getAllRowCount(String hql);    }

  2、DAO层实现类MemberDaoImpl对上面两个方法的实现如下:

public class MemberDaoImpl extends HibernateDaoSupport implements MemberDao {     //省略了其他的代码        /** //*    * 分页查询    * @param hql 查询的条件    * @param offset 开始记录    * @param length 一次查询几条记录    * @return    /   public List queryForPage(final String hql,final int offset,final int length){     List list = getHibernateTemplate().executeFind(new HibernateCallback(){       public Object doInHibernate(Session session) throws HibernateException,SQLException{         Query query = session.createQuery(hql);         query.setFirstResult(offset);         query.setMaxResults(length);         List list = query.list();         return list;       }     });     return list;   }         /* //*    * 查询所有记录数    * @return 总记录数    */   public int getAllRowCount(String hql){     return getHibernateTemplate().find(hql).size();   }    }

  3、下面我们来新建一个保存分页信息的类PageBean,具体代码如下:

public class PageBean {      private List list;    //要返回的某一页的记录列表      private int allRow;     //总记录数   private int totalPage;    //总页数   private int currentPage;  //当前页   private int pageSize;    //每页记录数      private boolean isFirstPage;  //是否为第一页   private boolean isLastPage;    //是否为最后一页   private boolean hasPreviousPage;  //是否有前一页   private boolean hasNextPage;    //是否有下一页         public List getList() {     return list;   }   public void setList(List list) {     this.list = list;   }   public int getAllRow() {     return allRow;   }   public void setAllRow(int allRow) {     this.allRow = allRow;   }   public int getTotalPage() {     return totalPage;   }   public void setTotalPage(int totalPage) {     this.totalPage = totalPage;   }   public int getCurrentPage() {     return currentPage;   }   public void setCurrentPage(int currentPage) {     this.currentPage = currentPage;   }   public int getPageSize() {     return pageSize;   }   public void setPageSize(int pageSize) {     this.pageSize = pageSize;   }      /** //*    * 初始化分页信息    /   public void init(){     this.isFirstPage = isFirstPage();     this.isLastPage = isLastPage();     this.hasPreviousPage = isHasPreviousPage();     this.hasNextPage = isHasNextPage();   }      /* //*    * 以下判断页的信息,只需getter方法(is方法)即可    * @return    /      public boolean isFirstPage() {     return currentPage == 1;  // 如是当前页是第1页   }   public boolean isLastPage() {     return currentPage == totalPage;  //如果当前页是最后一页   }   public boolean isHasPreviousPage() {     return currentPage != 1;    //只要当前页不是第1页   }   public boolean isHasNextPage() {     return currentPage != totalPage;  //只要当前页不是最后1页   }         /* //*    * 计算总页数,静态方法,供外部直接通过类名调用    * @param pageSize 每页记录数    * @param allRow 总记录数    * @return 总页数    /   public static int countTotalPage(final int pageSize,final int allRow){     int totalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize+1;     return totalPage;   }      /* //*    * 计算当前页开始记录    * @param pageSize 每页记录数    * @param currentPage 当前第几页    * @return 当前页开始记录号    /   public static int countOffset(final int pageSize,final int currentPage){     final int offset = pageSize(currentPage-1);     return offset;   }      /** //*    * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替    * @param page 传入的参数(可能为空,即0,则返回1)    * @return 当前页    */   public static int countCurrentPage(int page){     final int curPage = (page==0?1:page);     return curPage;   } }

 4、Service层接口的设计:

public interface MemberService {   //省略其他的代码      /** //*    * 分页查询    * @param currentPage 当前第几页    * @param pageSize 每页大小    * @return 封闭了分页信息(包括记录集list)的Bean    */   public PageBean queryForPage(int pageSize,int currentPage);    }

  5、Service层实现类的部分内码如下:

public class MemberServiceImpl implements MemberService {    //通过applicationContext.xml配置文件注入MemberDao的值   private MemberDao memberDao;   public void setMemberDao(MemberDao memberDao) {     this.memberDao = memberDao;   }      /** //*    * 分页查询    * @param currentPage 当前第几页    * @param pageSize 每页大小    * @return 封闭了分页信息(包括记录集list)的Bean    */   public PageBean queryForPage(int pageSize,int page){        final String hql = "from Member";    //查询语句     int allRow = memberDao.getAllRowCount(hql);  //总记录数     int totalPage = PageBean.countTotalPage(pageSize, allRow);  //总页数     final int offset = PageBean.countOffset(pageSize, page);  //当前页开始记录     final int length = pageSize;  //每页记录数     final int currentPage = PageBean.countCurrentPage(page);     List list = memberDao.queryForPage(hql,offset, length);    //"一页"的记录          //把分页信息保存到Bean中     PageBean pageBean = new PageBean();     pageBean.setPageSize(pageSize);       pageBean.setCurrentPage(currentPage);     pageBean.setAllRow(allRow);     pageBean.setTotalPage(totalPage);     pageBean.setList(list);     pageBean.init();     return pageBean;   }

 6、在Struts2中调用queryForPageMemberService层的queryForPage()方法即可return一个包含分页信息、符合条件的结果集list, 代码如下:

public class ListMember extends ActionSupport{   //通过applicationContext.xml配置文件注入memberService的值   private MemberService memberService;   public void setMemberService(MemberService memberService) {     this.memberService = memberService;   }      private int page;  //第几页      private PageBean pageBean;  //包含分布信息的bean      public int getPage() {     return page;   }      public void setPage(int page) {    //若URL中无此参数,会默认为第1页     this.page = page;   }      public PageBean getPageBean() {     return pageBean;   }      public void setPageBean(PageBean pageBean) {     this.pageBean = pageBean;   }      @Override   public String execute() throws Exception {     //分页的pageBean,参数pageSize表示每页显示记录数,page为当前页     this.pageBean = memberService.queryForPage(2, page);     return SUCCESS;   } }

  7、最后在listMember.jsp页面中,用到了Struts2标签:

<s:iterator value="pageBean.list">       <s:property value="title"/>       <a href="getArticle.action?id=<s:property value="id"/>">modify       <a href="deleteArticle.action?id=<s:property value="id"/>" onclick="return askDel()"/>delete     </s:iterator>     共<s:property value="pageBean.allRow"/> 条记录     共<s:property value="pageBean.totalPage"/> 页     当前第<s:property value="pageBean.currentPage"/>页          <s:if test="%{pageBean.currentPage == 1}">       第一页 上一页     </s:if>     <s:else>       第一页       <a href="listMyArticle.action?page=<s:property value="%{pageBean.currentPage-1}"/>">上一页     </s:else>     <s:if test="%{pageBean.currentPage != pageBean.totalPage}">       <a href="listMyArticle.action?page=<s:property value="%{pageBean.currentPage+1}"/>">下一页       <a href="listMyArticle.action?page=<s:property value="pageBean.totalPage"/>">最后一页     </s:if>     <s:else>       下一页 最后一页     </s:else>

评论(16)

  1. #16 楼 · qingfeng_101 · 2010-12-25 21:46

    呵呵 我也有个分页的东东 是个JavaBean+JSP标签的 使用起来很方便的哦 有空去看看吧

  2. #15 楼 · xiaoye4188 · 2010-08-02 17:47

    chrisx writes:

    我的个人组件包中关于分页是这么实现的 DAO: 使用的是我写的基于Spring, Hibernate的HibernateSupport再度封装了的泛型Dao,基于criteria查询,自己的DAO中只需要写组装条件。 Service, Action: 不用多说。 Web: 使用jsp自定义标签+jQuery实现通用分页工具栏与查询列表显示

    兄弟 贴一下代码 学下一下呀!

  3. #14 楼 · chrisx · 2010-06-21 17:17

    我的个人组件包中关于分页是这么实现的 DAO: 使用的是我写的基于Spring, Hibernate的HibernateSupport再度封装了的泛型Dao,基于criteria查询,自己的DAO中只需要写组装条件。 Service, Action: 不用多说。 Web: 使用jsp自定义标签+jQuery实现通用分页工具栏与查询列表显示

  4. #13 楼 · wulinshishen · 2010-05-24 00:39

    都是厉害的人物啊

  5. #12 楼 · maike · 2010-03-12 11:22

    进来看下struts2的

  6. #11 楼 · jsd117 · 2010-03-11 14:03

    我要好好看看。

  7. #10 楼 · storm0912 · 2010-01-27 00:36

    Springside 的分页就很好

  8. #9 楼 · srj2903 · 2010-01-26 22:58

    转被人的代码。

  9. #8 楼 · z95001188 · 2010-01-26 21:46

    代码太差劲了

  10. #7 楼 · sunrisetg · 2010-01-26 20:24

    上传个附件!

  11. #6 楼 · ivan18248 · 2010-01-26 15:21
    
    [quote=wangyan2009] 1、DAO层接口的设计,在MemberDao接口中定义了如下两个方法:
    
    public interface MemberDao{
      
      //省略了其他的代码
      
      /** *//**
       * 分页查询
       * @param hql 查询的条件
       * @param offset 开始记录
       * @param length 一次查询几条记录
       * @return
       */
      public List queryForPage(final String hql,final int offset,final int length);
      
      /** *//**
       * 查询所有记录数
       * @param hql 查询的条件
       * @return 总记录数
       */
      public int getAllRowCount(String hql);
      
    }
    
      2、DAO层实现类MemberDaoImpl对上面两个方法的实现如下:
    
    public class MemberDaoImpl extends HibernateDaoSupport implements MemberDao {
        //省略了其他的代码  
      
      /** *//**
       * 分页查询
       * @param hql 查询的条件
       * @param offset 开始记录
       * @param length 一次查询几条记录
       * @return
       */
      public List queryForPage(final String hql,final int offset,final int length){
        List list = getHibernateTemplate().executeFind(new HibernateCallback(){
          public Object doInHibernate(Session session) throws HibernateException,SQLException{
            Query query = session.createQuery(hql);
            query.setFirstResult(offset);
            query.setMaxResults(length);
            List list = query.list();
            return list;
          }
        });
        return list;
      }
      
      
      /** *//**
       * 查询所有记录数
       * @return 总记录数
       */
      public int getAllRowCount(String hql){
        return getHibernateTemplate().find(hql).size();
      }
      
    }
    
      3、下面我们来新建一个保存分页信息的类PageBean,具体代码如下:
    
    public class PageBean {
      
      private List list;    //要返回的某一页的记录列表
      
      private int allRow;     //总记录数
      private int totalPage;    //总页数
      private int currentPage;  //当前页
      private int pageSize;    //每页记录数
      
      private boolean isFirstPage;  //是否为第一页
      private boolean isLastPage;    //是否为最后一页
      private boolean hasPreviousPage;  //是否有前一页
      private boolean hasNextPage;    //是否有下一页
      
      
      public List getList() {
        return list;
      }
      public void setList(List list) {
        this.list = list;
      }
      public int getAllRow() {
        return allRow;
      }
      public void setAllRow(int allRow) {
        this.allRow = allRow;
      }
      public int getTotalPage() {
        return totalPage;
      }
      public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
      }
      public int getCurrentPage() {
        return currentPage;
      }
      public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
      }
      public int getPageSize() {
        return pageSize;
      }
      public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
      }
      
      /** *//**
       * 初始化分页信息
       */
      public void init(){
        this.isFirstPage = isFirstPage();
        this.isLastPage = isLastPage();
        this.hasPreviousPage = isHasPreviousPage();
        this.hasNextPage = isHasNextPage();
      }
      
      /** *//**
       * 以下判断页的信息,只需getter方法(is方法)即可
       * @return
       */
      
      public boolean isFirstPage() {
        return currentPage == 1;  // 如是当前页是第1页
      }
      public boolean isLastPage() {
        return currentPage == totalPage;  //如果当前页是最后一页
      }
      public boolean isHasPreviousPage() {
        return currentPage != 1;    //只要当前页不是第1页
      }
      public boolean isHasNextPage() {
        return currentPage != totalPage;  //只要当前页不是最后1页
      }
      
      
      /** *//**
       * 计算总页数,静态方法,供外部直接通过类名调用
       * @param pageSize 每页记录数
       * @param allRow 总记录数
       * @return 总页数
       */
      public static int countTotalPage(final int pageSize,final int allRow){
        int totalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize+1;
        return totalPage;
      }
      
      /** *//**
       * 计算当前页开始记录
       * @param pageSize 每页记录数
       * @param currentPage 当前第几页
       * @return 当前页开始记录号
       */
      public static int countOffset(final int pageSize,final int currentPage){
        final int offset = pageSize*(currentPage-1);
        return offset;
      }
      
      /** *//**
       * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替
       * @param page 传入的参数(可能为空,即0,则返回1)
       * @return 当前页
       */
      public static int countCurrentPage(int page){
        final int curPage = (page==0?1:page);
        return curPage;
      }
    }
    
     4、Service层接口的设计:
    
    public interface MemberService {
      //省略其他的代码
      
      /** *//**
       * 分页查询
       * @param currentPage 当前第几页
       * @param pageSize 每页大小
       * @return 封闭了分页信息(包括记录集list)的Bean
       */
      public PageBean queryForPage(int pageSize,int currentPage);
      
    }
    
      5、Service层实现类的部分内码如下:
    
    public class MemberServiceImpl implements MemberService {
     
     //通过applicationContext.xml配置文件注入MemberDao的值
      private MemberDao memberDao;
      public void setMemberDao(MemberDao memberDao) {
        this.memberDao = memberDao;
      }
      
      /** *//**
       * 分页查询
       * @param currentPage 当前第几页
       * @param pageSize 每页大小
       * @return 封闭了分页信息(包括记录集list)的Bean
       */
      public PageBean queryForPage(int pageSize,int page){
      
        final String hql = "from Member";    //查询语句
        int allRow = memberDao.getAllRowCount(hql);  //总记录数
        int totalPage = PageBean.countTotalPage(pageSize, allRow);  //总页数
        final int offset = PageBean.countOffset(pageSize, page);  //当前页开始记录
        final int length = pageSize;  //每页记录数
        final int currentPage = PageBean.countCurrentPage(page);
        List<Member> list = memberDao.queryForPage(hql,offset, length);    //"一页"的记录
        
        //把分页信息保存到Bean中
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);  
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
        return pageBean;
      }
    
     6、在Struts2中调用queryForPageMemberService层的queryForPage()方法即可return一个包含分页信息、符合条件的结果集list, 代码如下:
    
    public class ListMember extends ActionSupport{
      //通过applicationContext.xml配置文件注入memberService的值
      private MemberService memberService;
      public void setMemberService(MemberService memberService) {
        this.memberService = memberService;
      }
      
      private int page;  //第几页
      
      private PageBean pageBean;  //包含分布信息的bean
      
      public int getPage() {
        return page;
      }
      
      public void setPage(int page) {    //若URL中无此参数,会默认为第1页
        this.page = page;
      }
      
      public PageBean getPageBean() {
        return pageBean;
      }
      
      public void setPageBean(PageBean pageBean) {
        this.pageBean = pageBean;
      }
      
      @Override
      public String execute() throws Exception {
        //分页的pageBean,参数pageSize表示每页显示记录数,page为当前页
        this.pageBean = memberService.queryForPage(2, page);
        return SUCCESS;
      }
    }
    
      7、最后在listMember.jsp页面中,用到了Struts2标签:
    
    <s:iterator value="pageBean.list">
          <s:property value="title"/>
          <a href="getArticle.action?id=<s:property value="id"/>">modify</a>
          <a href="deleteArticle.action?id=<s:property value="id"/>" onclick="return askDel()"/>delete</a><br/>
        </s:iterator>
        共<s:property value="pageBean.allRow"/> 条记录
        共<s:property value="pageBean.totalPage"/> 页
        当前第<s:property value="pageBean.currentPage"/>页<br/>
        
        <s:if test="%{pageBean.currentPage == 1}">
          第一页 上一页
        </s:if>
        <s:else>
          <a href="listMyArticle.action?page=1">第一页</a>
          <a href="listMyArticle.action?page=<s:property value="%{pageBean.currentPage-1}"/>">上一页</a>
        </s:else>
        <s:if test="%{pageBean.currentPage != pageBean.totalPage}">
          <a href="listMyArticle.action?page=<s:property value="%{pageBean.currentPage+1}"/>">下一页</a>
          <a href="listMyArticle.action?page=<s:property value="pageBean.totalPage"/>">最后一页</a>
        </s:if>
        <s:else>
          下一页 最后一页
        </s:else>
    
    [/quote]
    
    
    
  12. #5 楼 · sing4j · 2010-01-26 13:30

    楼主的这个不是特别好,一楼的支持一下

  13. #4 楼 · zcq100 · 2010-01-26 13:26

    稍微复杂了,还可以再简

  14. #3 楼 · scd01234 · 2010-01-26 10:25

    赞同楼主做法。用spring 的 JdbcDaoSupport直接用SQL分页不是更快捷,方便?

  15. #2 楼 · dmewy · 2010-01-26 10:20

    通过hibernate分页还是很方便的..

  16. #1 楼 · 風一樣的男子 · 2010-01-26 10:00

    我也发发我的SSH分页

    dao 方法:

    
    /**
    	 * get appCodes by Pagination
    	 * 
    	 * @param appCode
    	 * @param pagination
    	 */
    	@SuppressWarnings("unchecked")
    	public Pagination getAppCodes(final AppCode appCode, final Pagination pagination) {
    		HibernateCallback callback = new HibernateCallback() {
    
    			@Override
    			public Object doInHibernate(Session session) throws HibernateException, SQLException {
    				int totalRow = 0;
    				List<AppCode> objList = new ArrayList<AppCode>();
    				try {
    					Criteria criteria = session.createCriteria(AppCode.class);
    					// 只查状态有效的
    					criteria.add(Restrictions.eq("status", CConstants.VALID));
    					totalRow = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
    					criteria.setProjection(null);
    					pagination.setTotalRow(totalRow);
    					objList = criteria.addOrder(Order.asc("category")).addOrder(Order.asc("codeId")).setFirstResult(pagination.getStartRow()).setMaxResults(pagination.getPageSize()).list();
    					pagination.setObjList(objList);
    				} catch (Exception e) {
    					log.error("uh oh, getappCodes by Pagination failed...");
    					e.printStackTrace();
    				}
    				return pagination;
    			}
    		};
    		return (Pagination) getHibernateTemplate().execute(callback);
    	}
    
    

    Service 方法:

    
    /**
    	 * get appCodes by Pagination
    	 * @param appCode
    	 * @param pagination
    	 */
    	public Pagination getAppCodes(AppCode appCode, Pagination pagination) {
    		return appCodeDao.getAppCodes(appCode, pagination);
    	}
    	
    	/**
    	 * get appCodes by Pagination for Flex
    	 * @param map
    	 * @param pagination
    	 */
    	public Pagination query(Map<String, String> map, Pagination pagination) {
    		if (map.isEmpty()) {
    			return getAppCodes(new AppCode(), pagination);
    		}
    		AppCode appCode = new AppCode();
    		if (StringUtils.isNotEmpty(map.get("id"))) {
    			appCode.setId(map.get("id"));
    		}
    		if (StringUtils.isNotEmpty(map.get("codeId"))) {
    			appCode.setCodeId(map.get("codeId"));
    		}
    		if (StringUtils.isNotEmpty(map.get("codeName"))) {
    			appCode.setCodeName(map.get("codeName"));
    		}
    		if (StringUtils.isNotEmpty(map.get("category"))) {
    			appCode.setCategory(map.get("category"));
    		}
    		if (StringUtils.isNotEmpty(map.get("categoryDesc"))) {
    			appCode.setCategoryDesc(map.get("categoryDesc"));
    		}
    		if (StringUtils.isNotEmpty(map.get("description"))) {
    			appCode.setDescription(map.get("description"));
    		}
    		if (StringUtils.isNotEmpty(map.get("status"))) {
    			appCode.setStatus(map.get("status"));
    		}
    		if (StringUtils.isNotEmpty(map.get("createDate"))) {
    			appCode.setCreateDate(DateTypeConverter.convertFromString(map.get("createDate")));
    		}
    		return getAppCodes(appCode, pagination);
    	}
    
    

    分页类:

    
    public class Pagination {
    	/** 当前页 */
    	private int currentPage = 1;
    	/** 每页显示数 */
    	private int pageSize = CConstants.PAGE_SIZE;
    	/** 总行数 */
    	private int totalRow = 0;
    	/** 页码 List */
    	private List<Integer> pages = new ArrayList<Integer>();
    	
    	/** 总页数 */
    	private int totalPage;
    	/** 当前页在数据库中的起始行 */
    	private int startRow=0;
    	
    	/** 查询参数保存 javabean的形式 */
    	private Object queryObject ;
    	
    	/**  要显示的数据集 */
        private List objList;
        
    	public Pagination() {
    	}
    
    	public Pagination(int currentPage, int pageSize, int totalRow) {
    		this.currentPage = currentPage;
    		this.pageSize = pageSize;
    		this.totalRow = totalRow;
    	}
    
    	public Pagination(int pageSize) {
    		this.pageSize = pageSize;
    		this.currentPage = 1;
    		this.totalRow = 1;
    	}
    
    	public int getCurrentPage() {
    		return currentPage;
    	}
    
    	public void setCurrentPage(int currentPage) {
    		if (currentPage==0) {
    			return;
    		}
    		this.currentPage = currentPage;
    	}
    
    	public int getPageSize() {
    		return pageSize;
    	}
    
    	public void setPageSize(int pageSize) {
    		this.pageSize = pageSize;
    	}
    
    	public int getTotalRow() {
    		return totalRow;
    	}
    
    	public void setTotalRow(int totalRow) {
    		this.totalRow = totalRow;
    	}
    
    	public int getStartRow() {
    		if (this.currentPage > 1) {
    			this.startRow = (this.currentPage - 1) * this.pageSize;
    		} else {
    			this.startRow = 0;
    		}
    		return startRow;
    	}
    
    	public void setStartRow(int startRow) {
    		this.startRow = startRow;
    	}
    
    	public List<Integer> getPages() {
    		this.pages.clear();
    		for (int i = 0; i < this.totalPage; i++) {
    			try {
    				this.pages.add((i+1));
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    		return pages;
    	}
    
    	public void setPages(List<Integer> pages) {
    		this.pages = pages;
    	}
    
    	public Object getQueryObject() {
    		return queryObject;
    	}
    
    	public void setQueryObject(Object queryObject) {
    		this.queryObject = queryObject;
    	}
    
    	public List getObjList() {
    		return objList;
    	}
    
    	public void setObjList(List objList) {
    		this.objList = objList;
    	}
    
    	public int getTotalPage() {
    		this.totalPage = this.totalRow / this.pageSize;
    		if (this.totalRow % this.pageSize != 0) {
    			this.totalPage += 1;
    		}
    		return totalPage;
    	}
    
    	public void setTotalPage(int totalPage) {
    		this.totalPage = totalPage;
    	}
    
    }
    
    

    Action 代码:

    
    @SuppressWarnings("serial")
    public class AppCodeAction extends BaseAction{
    
    	@Resource(name="appCodeService")
      private AppCodeService appCodeService;
      private AppCode appCode;
    
    	public AppCode getAppCode() {
    		return appCode;
    	}
    
    	public void setAppCode(AppCode appCode) {
    		this.appCode = appCode;
    	}
    
    	public String query() {
    		if (LOG.isDebugEnabled()) {
    			LOG.debug("Entering query method...");
    		}
    		if (this.pagination == null) {
    			this.pagination = new Pagination(CConstants.PAGE_SIZE);
    		}
    		if (this.appCode == null) {
    			this.appCode = new AppCode();
    		}
    		try{
    			this.pagination = appCodeService.getAppCodes(appCode, pagination);
    		}catch (Exception e) {
    			addActionError(getText("search.exception", new String []{getText("AppCode")}));
    			return ERROR;
    		}
    		return SUCCESS;
    	}
    }
    
    
    

    BaseAction

    
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.tjsoft.util.Pagination;
    @SuppressWarnings("serial")
    public class BaseAction extends ActionSupport {
    	
    	/** 分页类 */
    	protected Pagination pagination;
    
    	public Pagination getPagination() {
    		return pagination;
    	}
    
    	public void setPagination(Pagination pagination) {
    		this.pagination = pagination;
    	}
    	
    	public String preAdd(){
    		if (LOG.isDebugEnabled()) {
    			LOG.debug("preAdd query method...");
    		}
    		return INPUT;
    	}
    
    }
    
    

    分页导航:

    
    <%@ page contentType="text/html; charset=UTF-8"%>
    <%@ include file="/pages/taglibs.jsp" %>
    <s:if test="#request.pagination.totalPage>0">
        <div class="pageDiv" id="pageDiv">
        	<!-- 总记录数 
        	<span>${pagination.totalRow}</span>-->
        	<!-- 当前页/总页数 
        	<span>${pagination.currentPage}/${pagination.totalPage}</span>-->
        	<!-- 当前页不是第一页时生成首页和上一页 -->
        	<c:if test="${pagination.currentPage>1}">
    	    	<span onclick="doPage('1')"><a>首页</a></span>
        		<span onclick="doPage('pre')"><a>« 上一页</a></span>
        	</c:if>
        	<!-- 计算要分几次显示页码,每次显示10页 -->
        	<c:choose>
    	    	<c:when test="${pagination.totalPage %10 > 0}">
    	    		<c:set var="count" value="${pagination.totalPage/pagination.pageSize+1}" />
    	    	</c:when>
    	    	<c:otherwise>
    	    		<c:set var="count" value="${pagination.totalPage/pagination.pageSize}" />
    	    	</c:otherwise>
        	</c:choose>
        	<!-- count > 1 -->
        	<c:choose>
        		<c:when test="${count - 1 > 1}">
        			<!-- 1~10页先生成 -->
    	    		<label id="p_1" class="cruLabel">
    	    		<c:forEach var="p" begin="1" end="10">
    	    			<span onclick="doPage('${p}')" <c:if test="${p==pagination.currentPage}">class='selected'</c:if>><a>${p}</a></span>
    	    		</c:forEach>
    	    		<span onclick="doPageLabel('p_2')"><a> »</a></span>
    	    		</label>
    		    	<c:forEach var="index" begin="2" end="${count}">
    		    	<label id="p_${index}" class="hideLabel">
    		    		<span onclick="doPageLabel('p_${index-1}')"><a>« </a></span>
    			    	<c:forEach items="${pagination.pages}" var="page" begin="${(index-1)*pagination.pageSize}" end="${index*pagination.pageSize-1}">
    			    		<c:if test="${page>10}">
    					    	<span onclick="doPage('${page}')" <c:if test="${page==pagination.currentPage}">class='selected'</c:if>><a>${page}</a></span>
    			    		</c:if>
    			    	</c:forEach>
    			    	<c:if test="${count - index > 1}">
    			    		<span onclick="doPageLabel('p_${index+1}')"><a> »</a></span>
    			    	</c:if>
    		    	</label>
    		    	</c:forEach>
        		</c:when>
        		<c:otherwise>
    	    		<c:forEach items="${pagination.pages}" var="page">
    					<span onclick="doPage('${page}')" <c:if test="${page==pagination.currentPage}">class='selected'</c:if>><a>${page}</a></span>
    			    </c:forEach>
        		</c:otherwise>
        	</c:choose>
        	<!-- 当前页不是最后页时生成下一页 末页 -->
        	<c:if test="${pagination.currentPage!=pagination.totalPage}">
        		<span onclick="doPage('next')"><a>下一页 »</a></span>
        	<!--	<span onclick="doPage('${pagination.totalPage}')"><a>末页</a></span>-->
        	</c:if>
        	<!--<span>转到</span>
        	<select id="to_p">
        		<c:forEach items="${pagination.pages}" var="tp">
        			<option value="${tp}"
        				<c:if test="${tp eq pagination.currentPage}">
        				selected="selected"
        				</c:if>
        			>${tp}</option>
        		</c:forEach>
        	</select>
        	<span>页</span>
        	<span onclick="doPage(document.getElementById('to_p').value)">确定</span>-->
        </div>
      </s:if>
    <script type="text/javascript">
    /*** 翻页*/
    function doPage(page){
    	var form=document.forms[0];
    	if('${pagination.currentPage}'===page){
    		return;
    	}else if("next"===page){
    		if('${pagination.currentPage}'=='${pagination.totalPage}')return;
    		form.action='<s:url />?pagination.currentPage='+(new Number('${pagination.currentPage}')+1);
    	}else if("pre"===page){
    		if('${pagination.currentPage}'==1)return;
    		form.action='<s:url />?pagination.currentPage='+(new Number('${pagination.currentPage}')-1);
    	}else {
    		form.action='<s:url />?pagination.currentPage='+page;
    	}
    	form.submit();
    }
    </script>
    
    

评论功能维护中,当前仅展示历史评论。