Paging encapsulation Based on SSH + pager-taglib

Source: Internet
Author: User

Writing pages manually is good, but too tired. So in the search for a lot of open source paging plug-in on the Internet after selecting the pager-taglib-2.0, this paging plug-in because of simple usage powerful functions can quickly get started, the following gradually achieve the rapid jsp page pages.
Overall effect (because there are few records, there are only two pages ):

 

Page 2:

1. Download pager-taglib-2.0.war
In jsptags.com/tags/navigation/pager/ "> Configure.


2. You need to introduce
<% @ Taglib prefix = "pg" uri = "http://jsptags.com/tags/navigation/pager" %>

3. Create a JSPPageModel to return the object set and the total number of records on the jsp page.
Package com. v. oa. common;

Import java. util. List;

Public class JSPPageModel {

Private int total;

Private List list;

Public int getTotal (){
Return total;
}

Public void setTotal (int total ){
This. total = total;
}

Public List getList (){
Return list;
}

Public void setList (List list ){
This. list = list;
}

}
4. Create a SystemContext class to set the number of starting records and the number of lines displayed on each page to ThreadLocal.
Package com. v. oa. common;

Public class SystemContext {

Private static ThreadLocal offset = new ThreadLocal ();
Private static ThreadLocal pagesize = new ThreadLocal ();

Public static void setOffset (int _ offset ){
Offset. set (_ offset );
}

Public static int getOffset (){
Integer _ offset = (Integer) offset. get ();
If (_ offset = null ){
Return 0;
}
Return _ offset;
}

Public static void removeOffset (){
Offset. remove ();
}

Public static void setPagesize (int _ pagesize ){
Pagesize. set (_ pagesize );
}

Public static int getPagesize (){
Integer _ pagesize = (Integer) pagesize. get ();
If (_ pagesize = null ){
Return Integer. MAX_VALUE;
}
Return _ pagesize;
}

Public static void removePagesize (){
Pagesize. remove ();
}
}


5. Create a filter to read the number of starting records displayed in ThreadLocal and the number of lines displayed on each page. The page does not display the number of records displayed on each page. Five records are displayed on each page.
Package com. v. oa. common;

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 {

@ Override
Public void destroy (){
// TODO Auto-generated method stub

}

@ Override
Public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;

SystemContext. setOffset (getOffset (httpRequest ));
SystemContext. setPagesize (getPagesize (httpRequest ));

Try {
Chain. doFilter (request, response );
} Finally {
SystemContext. removeOffset ();
SystemContext. removePagesize ();
}
}

Private int getOffset (HttpServletRequest request ){
Int offset = 0;
Try {
Offset = Integer. parseInt (request. getParameter ("pager. offset "));
} Catch (Exception ignore ){
}
Return offset;
}

Private int getPagesize (HttpServletRequest request ){
Return 5;
}

Public void init (FilterConfig arg0) throws ServletException {
}
}

6. In action:
Protected ActionForward unspecified (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
OrgForm of = (OrgForm) form;
Int parentId = of. getParentId ();

& Nb

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.