Paging using the pager

Source: Internet
Author: User

Pager Jar URL: http://java2s.com/Code/Jar/t/Downloadtaglibspagejar.htm

Package Com.binary.entity;import Java.util.list;public class Pagemodel<t> {Private long total;//pages private List <T> dates;//The current page data 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 represents the beginning of the page to record public int getoffset () {return offset ;} public void SetOffset (int offset) {this.offset = offset;}}

 Packagecom.binary.entity;ImportJava.util.HashSet;ImportJava.util.Set;/*** User entity.@authormyeclipse Persistence Tools*/ Public classUserImplementsjava.io.Serializable {// Fields    PrivateInteger ID; PrivateString uname; PrivateString UPass; PrivateString Meun; //Constructors    /**default constructor*/     PublicUser () {}/**Minimal constructor*/     PublicUser (String meun) { This. Meun =Meun; }    /**Full constructor*/     PublicUser (String uname, String UPass, String Meun, Set meuns) { This. uname =uname;  This. UPass =UPass;  This. Meun =Meun; }    //Property Accessors     PublicInteger getId () {return  This. ID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicString Getuname () {return  This. uname; }     Public voidsetuname (String uname) { This. uname =uname; }     PublicString Getupass () {return  This. UPass; }     Public voidSetupass (String upass) { This. UPass =UPass; }     PublicString Getmeun () {return  This. Meun; }     Public voidSetmeun (String meun) { This. Meun =Meun; }}

Importjava.util.List;ImportOrg.hibernate.Query;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.Configuration;ImportCom.binary.entity.PageModel;ImportCom.binary.entity.User; Public classUserdao { PublicPagemodel<user> Getusers (intOffsetintMaxresult) {Configuration CF=NewConfiguration (). Configure (); Sessionfactory SF=cf.buildsessionfactory (); Session Session=sf.opensession (); Query Q= Session.createquery ("From User"); Pagemodel<User> users=NewPagemodel<user>();        Users.settotal (Q.list (). Size ());        Q.setfirstresult (offset);                Q.setmaxresults (Maxresult);        Users.setdates (Q.list ());        Session.close (); returnusers; }}

 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) {        returnnew  Userdao (). Getusers (offset, maxresult);}    }

 Packagecom.dan.action;ImportOrg.apache.struts2.ServletActionContext;ImportCom.binary.entity.PageModel;ImportCom.binary.entity.Pager;ImportCom.binary.entity.User;ImportCom.dan.biz.UserBiz;ImportCom.opensymphony.xwork2.ActionSupport; Public classUseractionextendsActionsupport {PrivatePager pager=NewPager ();//Storage Offset    Private intnumperpage=2;//amount of data per page    Private LongTotalCount;//Total Pages    PrivateString str;  PublicString getstr () {returnstr; }     Public voidsetstr (String str) { This. str =str; }     PublicPager Getpager () {returnPager; }     Public voidSetpager (Pager Pager) { This. Pager =Pager; }     Public intGetnumperpage () {returnNumperpage; }     Public voidSetnumperpage (intnumperpage) {         This. Numperpage =Numperpage; }     Public LongGettotalcount () {returnTotalCount; }     Public voidSettotalcount (Longtotalcount) {         This. TotalCount =TotalCount; }     PublicString Execute () {System.out.println (str); Userbiz Biz=Newuserbiz (); Pagemodel<User> users=biz.getusers (Pager.getoffset (), numperpage); TotalCount=users.gettotal (); Servletactioncontext.getrequest (). SetAttribute ("User", Users.getdates ()); returnSUCCESS; }}

Page.tag encapsulated as 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}" > Home </ a> </pg:first> <pg:prev> <a href= "${pageurl}" > front page </a> </ pg:prev> <pg:pages> <c:choose> <c:when test= "${pagenumber = = Curren                 Tpagenumber} "> <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" > Next </a> </pg:next> <pg:last&gt             ; <a href= "${pageurl}" > Last </a> </pg:last>${pageurl}</pg:pager>

JSP Code<%@ 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" >${u.uname}</c:forEach> <page:page color= "Red" numperpage= "${numperpage}" totalcount= "${totalcount}" ></page: Page></body> 

Paging using the pager

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.