Pager-taglib and filter are used in SSH for paging

Source: Internet
Author: User

In the SSH architecture, pager-taglib and filter are used to display pages based on user requirements. At this time, a user can select the number of rows to be displayed on each page based on the user's use in the drop-down table.
First, we will introduce the pager-taglib label:
PG: pager [this label is used to set the overall paging parameters] important parameter description:
URL: the root address of the paging link. The pager tag adds the paging parameter based on the link.
Items: Total number of records. The pager tag is based on this value to calculate the number of records to be queried for the paging parameter // you need to assign it the number of records to query.
Maxpageitems: number of lines displayed per page. The default value is 10.
Maxindexpages: the maximum number of pages to be output in a loop. The default value is 10.
 
PG: First [tab on the first page] important parameter description:
Significance of the export variable:
Pageurl
-Pagination URL (the most important export parameter)
Pagenumber-page number
Firstitem-
Index value of the first row of the Home Page
Lastitem-
Index value of the last row on the homepage
PG: Pre [Previous Page tag] important parameter description:
Significance of the export variable:
Pageurl-
URL address of the paging Link (the most important export parameter)
Pagenumber-page number
Firstitem-
Index value of the first row on the previous page
Lastitem-
Index value of the last row on the previous page
PG: Next [Next Page tag] important parameter description:
Significance of the export variable:
Pageurl-
URL address of the paging Link (the most important export parameter)
Pagenumber-page number
Firstitem-
Index value of the first row on the next page
Lastitem-
Index value of the last row on the next page
PG: last important parameter description:
Significance of the export variable:
Pageurl-
URL address of the paging Link (the most important export parameter)
Pagenumber-page number
Firstitem-
Index value of the first row of the last page
Lastitem-
Index value of the last row of the last page
PG: pages [this label is used to cyclically output page number information] important parameter description:
Significance of the export variable:
Pageurl-
URL address of the paging Link (the most important export parameter)
Pagenumber-page number
Firstitem-
The index value of the first row of the page specified by the page number.
Index value of the last row of the page specified by lastitem-pagenumber


By default, the pager-taglib component displays 10 entries per page. The corresponding attribute is maxpageitems = "10". Therefore, you can place your own number of displayed rows in the session, and assign it to maxpageitems to implement your own paging type. For example, httprequest. getsession (). setattribute ("Ps ",
10); then maxpageitems = "Ps"

The following is a specific example:
First download pager-taglib-2.0.war
, Address.

Generate a test table: t_person. The object class person has the ID, name, and phone attributes. The configuration in the hibernate configuration file is omitted...

The pagermodel class is used to store all persons queried from the database and the total number of records. The Code is as follows:
Package com. test;
Import java. util. List;
Public
Class pagermodel {
Private int total; // The total number of records.
Private list datas; // a collection of all users

... Get and set methods
}

The following class is used to obtain the number of displays per page and the number of first records used for query.

Package com. Test. filter;

Public class systemcontext {
Private Static
Threadlocal <integer> offset = new threadlocal <integer> (); // used for query. setfirstresult (offset)
Private Static
Threadlocal <integer> pagesize = new threadlocal <integer> (); // The number of lines displayed on each page of pagesize is used

Query. setmaxresults (pagesize); ----- A local variable of the threadlocal thread provides a copy of the variable value for each thread that uses the variable. Each thread can change its own copy independently, it does not conflict with copies of other threads, that is, every user can change the number of lines displayed on each page without affecting others.
 
Public
Static void setoffset (int
Offsetvalue ){
Offset. Set (offsetvalue );
}
Public static int
Getoffset (){
Integer ov = offset. Get ();
If (OV = NULL ){
Return
0;
}
Return ov;
}
 
Public static void setpagesize (int
Pagesizevalue ){
Pagesize. Set (pagesizevalue );
}
 
Public static
Int getpagesize (){
Integer PS = pagesize. Get ();
If (PS =
Null ){
Return integer. max_value; // set pagesize to infinity, that is, display on a page
}
Return
PS;
}
Public static void removeoffset () {// clear the copy assigned to each user
Offset. Remove ();
}

Public static void
Removepagesize (){
Pagesize. Remove ();
}
 
}
Pagerfilter: used to set the pagesize and offset for loading User-Defined pages.
Package com. Test. filter;

Import java. Io. ioexception;

Import javax. servlet. filter;
Import
Javax. servlet. filterchain;
Import javax. servlet. filterconfig;
Import
Javax. servlet. servletexception;
Import
Javax. servlet. servletrequest;
Import javax. servlet. servletresponse;
Import
Javax. servlet. http. httpservletrequest;

Public class pagerfilter implements Filter
{

Public static final string page_size_name =
"PS ";
 
Public void destroy (){

}
Public void
Dofilter (servletrequest request, servletresponse response,
Filterchain
Chain) throws ioexception, servletexception {

Httpservletrequest
Httprequest = (httpservletrequest) request;
// Set paging Parameters
Systemcontext. setoffset (getoffset (httprequest ));
Systemcontext. setpagesize (getpagesize (httprequest ));

Try {
Chain. dofilter (request,
Response );
} Finally {
Systemcontext. removeoffset ();
Systemcontext. removepagesize ();
}
}
Private
Int getoffset (httpservletrequest httprequest ){
Int offset = 0;
Try
{
Offset =
Integer. parseint (httprequest. getparameter ("pager. offset"); // obtain the pager. offset calculated by the tag.
}
Catch (exception ignore ){
}
Return offset;
}
// Obtain the number of lines displayed on each page
Private
Int getpagesize (httpservletrequest httprequest ){
String pagesize =
Httprequest. getparameter ("pagesize"); // obtain the number of lines displayed on each page.
If (pagesize! = NULL ){
Integer pS;
Try
{
PS =
Integer. parseint (pagesize );
Httprequest. getsession (). setattribute (page_size_name,
PS );
} Catch (exception ignore ){

}
}
Integer pagesize =
(Integer) httprequest. getsession (). getattribute (page_size_name );
If (pagesize
= NULL) {// if the user does not set it, 10 rows are displayed every day and put into the session for the purpose of assigning values to maxpageitems on the display page.
Httprequest. getsession (). setattribute (page_size_name,
10 );
Return 10;
} Else {
Return pagesize;
}
}

Public void Init (filterconfig arg0) throws
Servletexception {
 
}

}
Configure in Web. xml:
<Filter>

<Filter-Name> pagerfilter </filter-Name>

<Filter-class> com. Test. Filter. pagerfilter </filter-class>

</Filter>
<Filter-mapping>

<Filter-Name> pagerfilter </filter-Name>

<URL-pattern>/* </url-pattern>

</Filter-mapping>

The abstractmanager class is created to encapsulate the details of the query database. The management class can use searchpaginated (string
Hql) to get pagermodel:

Public class abstractmanager extends
Hibernatedaosupport {
 
Public pagermodel searchpaginated (string
Hql ){
Return searchpaginated (hql, offset, pagesize );
}
Public
Pagermodel searchpaginated (string hql, int offset, int
Pagesize ){

// Obtain the total number of records
String
Counthql = getcountquery (hql );
Query query =
Getsession (). createquery (counthql );
If (Params! = NULL &&
Params. length> 0 ){
For (INT I = 0; I <Params. length;
I ++ ){
Query. setparameter (I, Params [I]);
}
}
Int Total =
(Long) query. uniqueresult (). intvalue ();

// Obtain the result set of the current page
Query = getsession (). createquery (hql );
If (Params! =
Null & Params. length> 0 ){
For (INT I = 0; I <Params. length;
I ++ ){
Query. setparameter (I,
Params [I]);
}
}
Query. setfirstresult (systemcontext. getoffset (); // get the offset through systemcontext
Query. setmaxresults (systemcontext. getpagesize (); // get pagesize through systemcontext
List
Datas = query. List ();
Pagermodel PM = new
Pagermodel ();
PM. setdatas (datas );
PM. settotal (total );
Return
PM;
} // Obtain the total number of records based on the hql statement. The hql statement is as follows: select
... From... to select count (*) from ....
Private string getcountquery (string hql ){
Int Index
= Hql. indexof ("from ");
If (index! =-1 ){
Return "select count (*)" +
Hql. substring (INDEX );
}
Throw new
Systemexception ("invalid hql query statement! ");
}
}

// The following is a specific management class // obtain the pagination data pagermodel:
Public class
Personmanagerimpl extends abstractmanager implements personmanager
{// Personmanager is an interface
Public pagermodel searchpersons ()
{
Pagermodel PM = new pagermodel ();
String hql = "select P from person
P ";
Return this. searchpaginated (hql );
}
}

Create access page index. jsp
Main Code: <
Href = "person. Do"> User paging table </a>
Corresponding action page: Main Code:

Public class personaction extends dispatchaction
{

Private personmanager
Personmanager; // inject the person management class using spring
@ Override
Protected actionforward
Unspecified (actionmapping mapping, actionform form,
Httpservletrequest
Request, httpservletresponse response)
Throws exception
{

Pagermodel PM =
Personmanager. searchpersons ();
Request. setattribute ("PM", PM );
Return
Mapping. findforward ("Index"); // go to list. jsp through the configuration file
}
Public void
Setpersonmanager (personmanager ){
This. personmanager =
Personmanager;
}

}
Display page: List. jsp
<% @ Taglib prefix = "PG"
Uri = "http://jsptags.com/tags/navigation/pager" %> // Import
...
<SCRIPT type = "text/JavaScript">
Function
Selectpagesize (field ){
Document. Location. href =
Document. All. firstpageurl. href + "& pagesize =" + field. value; // obtain the number of lines displayed on each page selected from the drop-down list, and refresh to go to the homepage.
}
</SCRIPT>
....

<Table>
<! -- List title bar
-->
<Tr
Bgcolor = "# eff3f7" class = "tablebody1">

<TD
> Name </TD>
<TD
> Telephone </TD>

</Tr>
<! -- List data bar
-->
<C: foreach
Items = "$ {pm. datas}" Var = "person">
<Tr>
<TD
>$ {Person. name} </TD>
<TD> $ {person. Phone
} </TD>
</Tr>
</C: foreach>

</Table>
<Table>
<Tbody>

<Tr>
<TD>
<! -- Pagination navigation bar -->
<PG: pager url = "person. Do" items = "$ {pm. Total }"
Export = "currentpagenumber = pagenumber" maxpageitems = "$ {PS}"> // obtain the number of lines displayed on each page within the session range.
<PG: First>
<A href = "$ {pageurl }"
Id = "firstpageurl"> homepage </a>
</PG: First>
<PG: Prev>
<
Href = "$ {pageurl
} "> Previous page </a>
</PG: Prev>
<PG: pages>
<C: Choose>
<C: When
Test = "$ {currentpagenumber EQ pagenumber}">
<Font
Color = "red" >$ {pagenumber} </font> // the link is not displayed for the current page number.
</C: When>
<C: otherwise>
<
Href = "$ {pageurl}" >$ {pagenumber
} </A>
</C: otherwise>
</C: Choose>
</PG: pages>
<PG: Next>
<
Href = "$ {pageurl
} "> Back page </a>
</PG: Next>
<PG: Last>
<
Href = "$ {pageurl
} "> Last page </a>
</PG: Last>
</PG: pager>
<! -- Select the number of lines displayed per page drop-down list -->
Display per page
<Select
Name = "pagesize" onchange = "selectpagesize (this)">
<C: foreach begin = "5"
End = "50" step = "5" Var = "I">
<Option value = "$ {I }"
<C: If
Test = "$ {PS EQ I
} "> Selected </C: If>
>$ {I} </option>
</C: foreach>
</SELECT> row

</TD>
</Tr>
</Tbody>

</Table>

The implementation diagram is as follows:

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.