* @ Author zhangshuling E-mail: zhangshuling1214@126.com
* @ Version createtime: Apr 2, 2009 9:20:07 AM for the integration of such pagination ajax to return a
* Java.util.Hashtable there were two objects have a string of pagination (<<<1 | 2 | 3 | 4 | 5 |
* .....>>>) A data page (java.util.List) did not make the time being the related Hibernate query, Tim has required its own right
* ^ _ ^
* /
public class AjaxPage (
private int pageSize = 10; / / Pagination scale (recorded as the number of page)
private int pageTotal = 0; / / Pagination total (the total number of page 1)
/ / Pagination required picture
private String firstPageImage = "http://www.kaogoo.com/english/images/back2.gif";
private String previousPageImage = "http://www.kaogoo.com/english/images/backing.gif";
private String nextPageImage = "http://www.kaogoo.com/english/images/next.gif";
private String lastPageImage = "http://www.kaogoo.com/english/images/next2.gif";
public static final String KEY_PAGELIST = "PAGELIST";
public static final String KEY_PAGELINK = "PAGELINK";
public AjaxPage () (
)
public AjaxPage (int pageSize, int pageTotal, String firstPageImage,
String previousPageImage, String nextPageImage, String lastPageImage) (
setPageSize (pageSize);
setPageTotal (pageTotal);
setFirstPageImage (firstPageImage);
setPreviousPageImage (previousPageImage);
setNextPageImage (nextPageImage);
setLastPageImage (lastPageImage);
)
public String getFirstPageImage () (
return firstPageImage;
)
public void setFirstPageImage (String firstPageImage) (
this.firstPageImage = firstPageImage;
)
public String getLastPageImage () (
return lastPageImage;
)
public void setLastPageImage (String lastPageImage) (
this.lastPageImage = lastPageImage;
)
public String getNextPageImage () (
return nextPageImage;
)
public void setNextPageImage (String nextPageImage) (
this.nextPageImage = nextPageImage;
)
public int getPageSize () (
return pageSize;
)
public void setPageSize (int pageSize) (
this.pageSize = pageSize;
)
public int getPageTotal () (
return pageTotal;
)
public void setPageTotal (int pageTotal) (
this.pageTotal = pageTotal;
)
public String getPreviousPageImage () (
return previousPageImage;
)
public void setPreviousPageImage (String previousPageImage) (
this.previousPageImage = previousPageImage;
)
/ **
*
* @ Param sqlTotal
* / / Query all the number of records
* @ Param sql
* / / Inquiries Record
* @ Param currentPage
* / / Current page number
* @ Param parameter
* / / Parameters () parameter brought about by the & & must be escaped in the transfer or at the wrong time & parameter = 1 & ...
* Should be written & parameter = 1 & ...
* @ Param ajaxMethod
* / / AjaxMethod referring to the javascript method call ajax format
* AjaxMethod (parameter, page);
* @ Param connection
* @ Param preparedStatement
* @ Param resultSet
* @ Return
* /
public java.util.Hashtable ajaxPage (String sqlTotal, String sql,
int currentPage, String parameter, String ajaxMethod,
java.sql.Connection connection,
java.sql.PreparedStatement preparedStatement,
java.sql.ResultSet resultSet) (
java.util.Hashtable pageContent = new java.util.Hashtable ();
java.util.List pageList = new java.util.ArrayList ();
StringBuffer pageLink = new StringBuffer (
"<table width="100%" align="center" cellspacing="0" cellpadding="0">");
try (
preparedStatement = connection.prepareStatement (sqlTotal);
resultSet = preparedStatement.executeQuery ();
resultSet.next ();
setPageTotal (resultSet.getInt (1));
) Catch (java.sql.SQLException e) (
setPageTotal (0);
) Catch (Exception e) (
setPageTotal (0);
)
if (getPageTotal ()> 0) (
int pageCount = (int) Math.ceil ((double) getPageTotal ()
/ GetPageSize ()); / / total number of the sub-page
int start = currentPage - 4;
int end = currentPage + 4;
if (start <= 1) (
start = 1;
end = start + 9;
)
if (end> = pageCount) (
end = pageCount;
start = end - 9;
if (start <= 0) (
start = 1;
)
)
int previous = (currentPage - 1> 0? (currentPage - 1): 1);
int next = ((currentPage + 1) <pageCount? (currentPage + 1)
: PageCount);
pageLink
. append ( "<tr> <td align="center" height="30" width="100%">");
pageLink.append ( "<a href =" javascript: "). append (ajaxMethod)
. append ("('"). append (parameter). append ( " ', 1)"). append (
"" title = "Go to Page 1"> ");
pageLink.append ( "<img boder =" 0 "src ="");
pageLink.append (getFirstPageImage ())
. append (""/></ a> ");
pageLink.append ( "");
pageLink.append ( "<a href =" javascript: "). append (ajaxMethod)
. append ("('"). append (parameter). append ("',"). append (
previous). append ( ")" title = "go"). append (
previous). append ( "pages"> ");
pageLink.append ( "<img boder =" 0 "src ="");
pageLink.append (getPreviousPageImage ()). append (
""/></ a> ");
pageLink.append ( "");
for (int i = start; i <= end; i + +) (
if (i! = currentPage) (
pageLink.append ( "<a href =" javascript: "). append (
ajaxMethod). append ("('"). append (parameter). append (
"',"). append (i). append ( ")" title = "Go")
. append (i). append ( "page ">"). append (i). append (
"</ a>");
) Else (
pageLink
. append ( "<span title =" Current ")
. append (i)
. append (
"Page"> <font color="red"> ")
. append (i). append ( "</ font> </ span>");
)
if (i <end) (
pageLink.append ( "|");
)
)
pageLink.append ( "");
pageLink.append ( "<a href =" javascript: "). append (ajaxMethod)
. append ("('"). append (parameter). append ("',"). append (next)
. append ( ")" title = "go"). append (next). append (
"Pages"> ");
pageLink.append ( "<img boder =" 0 "src ="");
pageLink.append (getNextPageImage ()). append (""/></ a> ");
pageLink.append ( "");
pageLink.append ( "<a href =" javascript: "). append (ajaxMethod)
. append ("('"). append (parameter). append ("',"). append (
pageCount). append ( ")" title = "Go")
. append (pageCount). append ( "pages"> ");
pageLink.append ( "<img boder =" 0 "src ="");
pageLink.append (getLastPageImage ()). append (""/></ a> ");
pageLink.append ( "");
pageLink.append ( "</ td> </ tr>");
if (currentPage - 1 <previous) (
currentPage = previous;
)
if (currentPage + 1> next) (
currentPage = next;
)
int focus = ((currentPage - 1) * getPageSize ()) + 1;
try (
preparedStatement = connection.prepareStatement (sql);
resultSet = preparedStatement.executeQuery ();
pageList = parseResultSet (pageList, resultSet, this
. getPageSize (), focus);
) Catch (java.sql.SQLException e) (
setPageTotal (0);
) Catch (Exception e) (
setPageTotal (0);
)
) Else (/ / total is 0 No Record Record
pageLink
. append ( "<tr> <td align="center" width="100%">");
pageLink.append ( "did not meet the requirements of data!");
pageLink.append ( "</ td> </ tr>");
)
pageLink.append ( "</ table>");
pageContent.put (AjaxPage.KEY_PAGELIST, pageList);
pageContent.put (AjaxPage.KEY_PAGELINK, pageLink.toString ());
return pageContent;
)
/ **
* ResultSet set a record store into a Object [] of the List
*
* @ Param pageList
* @ Param resultSet
* @ Param size
* / / Check the number of Record
* @ Param focus
* / / Start from the first few go
* @ Return
* /
public java.util.List parseResultSet (java.util.List pageList,
java.sql.ResultSet resultSet, int size, int focus) (
try (
resultSet.absolute (focus); / / position to the first number of grid
resultSet.previous (); / / forward one grid
int col = resultSet.getMetaData (). getColumnCount ();
Object [] obj;
int index = 1;
while (resultSet.next ()) (
if (index> size)
break;
obj = new Object [col];
for (int i = 1; i <= col; i + +) (
obj [(i - 1)] = resultSet.getString (i);
)
pageList.add (obj);
index + +;
)
) Catch (java.sql.SQLException e) (
e.printStackTrace ();
) Catch (Exception e) (
e.printStackTrace ();
)
return pageList;
)
)







