Demonstration of jsp digital paging class jdbcTemplate

Source: Internet
Author: User


The first file below is pageSupport mainly processing various parameters.

Package net. spring. service;
Import java. util. List;
Public class pageSupport {
Int pagesize = 2; // number of entries per page
Int totalCount; // The total number of items.
Int curPage; // the current page number.
Int pageCount; // the total number of pages.
List datas; // record set
Public pageSupport (int startpage, int pagesizes, int count ){
 
This. setPagesize (pagesizes );
This. setTotalCount (count );
This. pageCount = count/pagesizes + 1;
This. setCurPage (startpage );
}
Public int getPageCount (){
Return pageCount;
}
Public void setPageCount (int pageCount ){
This. pageCount = pageCount;
}
Public int getPagesize (){
Return pagesize;
}
Public void setPagesize (int pagesize ){
This. pagesize = pagesize;
}
Public int getTotalCount (){
Return totalCount;
}
Public void setTotalCount (int totalCount ){
This. totalCount = totalCount;
}
Public int getCurPage (){
Return curPage;
}
Public void setCurPage (int curPage ){
If (curPage <= 0)
CurPage = 0;
If (curPage> this. getPageCount ())
CurPage = this. getPageCount ();
This. curPage = curPage;
}
Public List getDatas (){
Return datas;
}
Public void setDatas (List datas ){
This. datas = datas;
}
 
}
 

This is the call Method

Passed in the current page http://index.html when calling? Page = 2. For example, the input parameter "page" corresponds to the variable "startpage ".

[Html]

 
Int startpage = Integer. parseIn (response. Parameter (page), pagesize = 5
// This. geCount () is a function that obtains the total number of records and writes it by yourself.
PageSupport pSupport = new pageSupport (startpage, pagesize, this. getCount ());
String SQL = "select * from table limit ?,? ";
List list = // here you need to read the database records and return the list form jdbcTemplate. queryForList (SQL,
New Object [] {(pSupport. getCurPage ()-1) * pagesize
, Pagesize });
PSupport. setDatas (list );
[Html] view plaincopy
<% -- Digit paging start -- %>
<%!
// @ ListNum = 3; bottom 1 2 3, etc.
// @ ThePageUrl: url patchwork string. For example: request. getRequestURI () + "? Page = ";
// @ PageClass = "Page": When the Page number CSS class
// @ PageCount: Total number of pages
// @ ShowPage: the number of pages to display
Public String getPage (int listNum, String thePageUrl, String pageClass, int pageCount, int showPage)
{
String result = "";
If (pageCount <= listNum)
{
For (int I = 1; I <= pageCount; I ++)
{
If (I = showPage)
{
Result = result + "<span class =" + pageClass + ">" + I + "</span> ";
}
Else
{
Result = result + "<a href =" + thePageUrl + I + "> [" + I + "] </a> ";
}
}
Return result;
}
Else if (pageCount> listNum)
{
// Judge based on showPage
Int FromListNum;
If (showPage % listNum! = 0) // here, based on the remainder, determine which page the current page should be in, for example, 6-10 in 1-5.
{FromListNum = (showPage/listNum) * listNum + 1 ;}
Else
{FromListNum = (showPage/listNum) * listNum-listNum + 1 ;}
Int ToListNum = FromListNum + listNum-1;
If (pageCount> = ToListNum)
{
If (FromListNum! = 1)
{
Result = result + "<a href =" + thePageUrl + "1 title = Home> <FONT face = webdings> 9 </FONT> </a> ";
Result = result + "<a href =" + thePageUrl + (FromListNum-1) + "title = Top" + listNum + "Page> <FONT face = webdings> 7 </FONT> </a> ";
}
Else
{
Result = result + "<FONT face = webdings> 9 </FONT> ";
Result = result + "<FONT face = webdings> 7 </FONT> ";
}
For (int I = FromListNum; I <= ToListNum; I ++)
{
If (I = showPage)
{
Result = result + "<span class =" + pageClass + ">" + I + "</span> ";
}
Else
{
Result = result + "<a href =" + thePageUrl + I + "> [" + I + "] </a> ";
}
}
Result = result + "<a href =" + thePageUrl + (ToListNum + 1) + "title = bottom" + listNum + "Page> <FONT face = webdings> 8 </FONT> </a> ";
Result = result + "<a href =" + thePageUrl + pageCount + "title = last page> <FONT face = webdings >:</FONT> </a> ";
Return result;
} Else
{
Result = result + "<a href =" + thePageUrl + "1 title = Home> <FONT face = webdings> 9 </FONT> </a> ";
Result = result + "<a href =" + thePageUrl + (FromListNum-1) + "title = Top" + listNum + "Page> <FONT face = webdings> 7 </FONT> </a> ";
For (int I = FromListNum; I <= pageCount; I ++)
{
If (I = showPage)
{
Result = result + "<span class =" + pageClass + ">" + I + "</span> ";
}
Else
{
Result = result + "<a href =" + thePageUrl + I + "> [" + I + "] </a> ";
}
}
Result = result + "<FONT face = webdings> 8 </FONT> ";
Result = result + "<FONT face = webdings >:</FONT> ";
Return result;
}
}
Return "";
}
%>
<% -- End by number in paging form -- %>

Related Article

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.