使用pager进行分页

系统 1607 0

pager jar网址:http://java2s.com/Code/Jar/t/Downloadtaglibspagejar.htm

 

      package com.binary.entity;



import java.util.List;



public class PageModel<T> {



	private long total;//页数

	private List<T> dates;//当前页的数据

	public long getTotal() {

		return total;

	}

	public void setTotal(long total) {

		this.total = total;

	}

	public List<T> getDates() {

		return dates;

	}

	public void setDates(List<T> dates) {

		this.dates = dates;

	}

	

}


    

 

      package com.binary.entity;



public class Pager {



	private int offset;//offset表示从那一页开始记录



	public int getOffset() {

		return offset;

	}



	public void setOffset(int offset) {

		this.offset = offset;

	}

	

}


    

 

 

      
        package
      
      
         com.binary.entity;




      
      
        import
      
      
         java.util.HashSet;


      
      
        import
      
      
         java.util.Set;




      
      
        /**
      
      
        

 * User entity. 
      
      
        @author
      
      
         MyEclipse Persistence Tools

 
      
      
        */
      
      
        public
      
      
        class
      
       User 
      
        implements
      
      
         java.io.Serializable {



    
      
      
        //
      
      
         Fields
      
      
        private
      
      
         Integer id;

    
      
      
        private
      
      
         String uname;

    
      
      
        private
      
      
         String upass;

    
      
      
        private
      
      
         String meun;



    
      
      
        //
      
      
         Constructors
      
      
        /**
      
      
         default constructor 
      
      
        */
      
      
        public
      
      
         User() {

    }



    
      
      
        /**
      
      
         minimal constructor 
      
      
        */
      
      
        public
      
      
         User(String meun) {

        
      
      
        this
      
      .meun =
      
         meun;

    }



    
      
      
        /**
      
      
         full constructor 
      
      
        */
      
      
        public
      
      
         User(String uname, String upass, String meun, Set meuns) {

        
      
      
        this
      
      .uname =
      
         uname;

        
      
      
        this
      
      .upass =
      
         upass;

        
      
      
        this
      
      .meun =
      
         meun;

    }



    
      
      
        //
      
      
         Property accessors
      
      
        public
      
      
         Integer getId() {

        
      
      
        return
      
      
        this
      
      
        .id;

    }



    
      
      
        public
      
      
        void
      
      
         setId(Integer id) {

        
      
      
        this
      
      .id =
      
         id;

    }



    
      
      
        public
      
      
         String getUname() {

        
      
      
        return
      
      
        this
      
      
        .uname;

    }



    
      
      
        public
      
      
        void
      
      
         setUname(String uname) {

        
      
      
        this
      
      .uname =
      
         uname;

    }



    
      
      
        public
      
      
         String getUpass() {

        
      
      
        return
      
      
        this
      
      
        .upass;

    }



    
      
      
        public
      
      
        void
      
      
         setUpass(String upass) {

        
      
      
        this
      
      .upass =
      
         upass;

    }



    
      
      
        public
      
      
         String getMeun() {

        
      
      
        return
      
      
        this
      
      
        .meun;

    }



    
      
      
        public
      
      
        void
      
      
         setMeun(String meun) {

        
      
      
        this
      
      .meun =
      
         meun;

    }



}
      
    

 

 

      
        import
      
      
         java.util.List;




      
      
        import
      
      
         org.hibernate.Query;


      
      
        import
      
      
         org.hibernate.Session;


      
      
        import
      
      
         org.hibernate.SessionFactory;


      
      
        import
      
      
         org.hibernate.cfg.Configuration;




      
      
        import
      
      
         com.binary.entity.PageModel;


      
      
        import
      
      
         com.binary.entity.User;




      
      
        public
      
      
        class
      
      
         UserDao {



    
      
      
        public
      
       PageModel<User> getUsers(
      
        int
      
       offset,
      
        int
      
      
         maxResult) {

        Configuration cf
      
      =
      
        new
      
      
         Configuration().configure();

        SessionFactory sf
      
      =
      
        cf.buildSessionFactory();

        Session session
      
      =
      
        sf.openSession();

        Query q
      
      = session.createQuery("from User"
      
        );

        PageModel
      
      <User> users=
      
        new
      
       PageModel<User>
      
        ();

        users.setTotal(q.list().size());

        q.setFirstResult(offset);

        q.setMaxResults(maxResult);

        

        users.setDates(q.list());

        session.close();

        
      
      
        return
      
      
         users;

    }

}
      
    

 

 

      
        package
      
      
         com.dan.biz;




      
      
        import
      
      
         com.binary.entity.PageModel;


      
      
        import
      
      
         com.binary.entity.User;


      
      
        import
      
      
         com.dan.dao.UserDao;




      
      
        public
      
      
        class
      
      
         UserBiz {



    
      
      
        public
      
       PageModel<User> getUsers(
      
        int
      
       offset,
      
        int
      
      
         maxResult) {

        
      
      
        return
      
      
        new
      
      
         UserDao().getUsers(offset, maxResult);

    }

}
      
    

 

 

      
        package
      
      
         com.dan.action;




      
      
        import
      
      
         org.apache.struts2.ServletActionContext;




      
      
        import
      
      
         com.binary.entity.PageModel;


      
      
        import
      
      
         com.binary.entity.Pager;


      
      
        import
      
      
         com.binary.entity.User;


      
      
        import
      
      
         com.dan.biz.UserBiz;


      
      
        import
      
      
         com.opensymphony.xwork2.ActionSupport;




      
      
        public
      
      
        class
      
       UserAction 
      
        extends
      
      
         ActionSupport {



    

    
      
      
        private
      
       Pager pager=
      
        new
      
       Pager();
      
        //
      
      
        存放偏移量
      
      
        private
      
      
        int
      
       numPerPage=2;
      
        //
      
      
        每页的数据量
      
      
        private
      
      
        long
      
       totalCount;
      
        //
      
      
        总页数
      
      
        private
      
      
         String str;

    

    

    
      
      
        public
      
      
         String getStr() {

        
      
      
        return
      
      
         str;

    }



    
      
      
        public
      
      
        void
      
      
         setStr(String str) {

        
      
      
        this
      
      .str =
      
         str;

    }



    
      
      
        public
      
      
         Pager getPager() {

        
      
      
        return
      
      
         pager;

    }



    
      
      
        public
      
      
        void
      
      
         setPager(Pager pager) {

        
      
      
        this
      
      .pager =
      
         pager;

    }



    
      
      
        public
      
      
        int
      
      
         getNumPerPage() {

        
      
      
        return
      
      
         numPerPage;

    }



    
      
      
        public
      
      
        void
      
       setNumPerPage(
      
        int
      
      
         numPerPage) {

        
      
      
        this
      
      .numPerPage =
      
         numPerPage;

    }



    
      
      
        public
      
      
        long
      
      
         getTotalCount() {

        
      
      
        return
      
      
         totalCount;

    }



    
      
      
        public
      
      
        void
      
       setTotalCount(
      
        long
      
      
         totalCount) {

        
      
      
        this
      
      .totalCount =
      
         totalCount;

    }



    
      
      
        public
      
      
         String execute() {

        System.out.println(str);

        UserBiz biz
      
      =
      
        new
      
      
         UserBiz();

        PageModel
      
      <User> users=
      
        biz.getUsers(pager.getOffset(), numPerPage);

        totalCount
      
      =
      
        users.getTotal();

        ServletActionContext.getRequest().setAttribute(
      
      "user"
      
        , users.getDates());

        
      
      
        return
      
      
         SUCCESS;

    }

}
      
    

 

      
        page.tag封装成tag标签


      
      <%@tag pageEncoding="utf-8" %>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="pg" uri="/WEB-INF/lib/pager-taglib.jar" %>

<%@attribute name="color" required="true" %>

<%@attribute name="totalCount" required="true" rtexprvalue="true" %>

<%@attribute name="numPerPage" required="true" rtexprvalue="true" %>



<pg:pager items="${totalCount }" url="user" export="currentPageNumber=pageNumber"
      
        

     maxPageItems
      
      ="${numPerPage }" maxIndexPages="5"> 

         

         <pg:first>

             <a href="${pageUrl }">首页</a>

         </pg:first>

         <pg:prev>

             <a href="${pageUrl }">前页</a>

         </pg:prev>

         <pg:pages>

             <c:choose>

                 <c:when test="${pageNumber ==  currentPageNumber}">

                     <font color="red">${pageNumber }</font>

                 </c:when>

                 <c:otherwise>

                     <a href="${pageUrl }">${pageNumber }</a>

                 </c:otherwise>

             </c:choose>

             

         </pg:pages>

         <pg:next>

             <a href="${pageUrl }&str=aaaa">下一页</a>

             

         </pg:next>

         <pg:last>

             <a href="${pageUrl }">尾页</a>

         </pg:last>
      
        

         ${pageUrl }

         

     
      
      </pg:pager>
    

 

 

      
        jsp代码


      
      <%@ page language="java" 
      
        import
      
      ="java.util.*" pageEncoding="utf-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="pg" uri="/WEB-INF/lib/pager-taglib.jar" %>

<%@ taglib prefix="page" tagdir="/WEB-INF/tags" %>

<%
      
        

String path 
      
      =
      
         request.getContextPath();

String basePath 
      
      = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"
      
        ;


      
      %>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'index.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">    

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

  </head>

 

</head> 

<body> 

    <c:forEach items="${user }" var="u">
      
        

        ${u.uname }

    
      
      </c:forEach>



    <page:page color="red" numPerPage="${numPerPage }" totalCount="${totalCount }"></page:page>

</body> 

</html> 
    

 

使用pager进行分页


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论