package com.myecsun.util.helper;
import org.hibernate.Query;
import java.util.List;
import org.hibernate.Criteria;
//讀取一頁數(shù)據(jù)的通用類
/**================================
===================================*/
public class Page {
private List results;
private int pageSize;
private int page;
public Page() {
}
public Page(Query query,int pageSize,int page){
this.page=page;
this.pageSize=pageSize;
results=query.setFirstResult((page-1)*pageSize).setMaxResults(pageSize).list();
}
public Page(Criteria crit,int pageSize,int page){
this.page=page;
this.pageSize=pageSize;
results=crit.setFirstResult((page-1)*pageSize).setMaxResults(pageSize).list();
}
public boolean isHaveNextPage(){
return results.size()>pageSize;
}
public boolean isHavePreviousPage(){
return page>0;
}
public List getResultsList(){
return isHaveNextPage()?results.subList(0,pageSize-1):results;
}
}
本類在DAO中的使用方法為:
public Page getPosts(int pageSize, int page) {
return new Page(this.getSession().getNamedQuery("getGroups"),pageSize,page);
}
傳入的參數(shù)分別為:一頁顯示數(shù)據(jù)的條數(shù)和當前頁號
編寫標簽輔助類:
package com.myecsun.util.helper;
import javax.servlet.http.HttpServletRequest;
public class PageHelper {
public PageHelper() {
}
public OldPage getPageInfo(HttpServletRequest request, int total) {
try {
OldPage page = new OldPage();
int pageSize = 25;
String pageno = request.getParameter("pageno");
String totalcount = request.getParameter("totalcount");
String totalpage = request.getParameter("totalpage");
page.setPagesize(pageSize);
if (pageno == null || totalcount == null || totalpage == null) {
page.setPageno(1);
page.setTotalcount(total);
if (total % pageSize == 0) {
page.setTotalpage(total / pageSize);
}
else {
page.setTotalpage(total / pageSize + 1);
}
}
else {
page.setPageno(Integer.parseInt(pageno));
page.setTotalcount(Integer.parseInt(totalcount));
page.setTotalpage(Integer.parseInt(totalpage));
}
return page;
}
catch (NumberFormatException ex) {
System.out.println("ex is :" + ex.toString());
return null;
}
}
}
在標簽中使用標簽輔助類:
package com.myecsun.util.helper;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
*/
public class PageTag
extends SimpleTagSupport {
private OldPage page;
private String action = null;
// private String nodeID;
public void setAction(String action) {
this.action = action;
}
public String getAction() {
return this.action;
}
public OldPage getPage() {
return this.page;
}
// public String getNodeID() {
// return nodeID;
// }
public void setPage(OldPage page) {
this.page = page;
}
// public void setNodeID(String nodeID) {
// this.nodeID = nodeID;
// }
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
int totalpage = page.getTotalpage();
int totalcount = page.getTotalcount();
int pageno = page.getPageno();
int addpageno = pageno + 1;
int minpageno = pageno - 1;
int pageStart;
int pageEnd;
if (pageno - 4 > 0) {
pageStart = pageno - 3;
if (totalpage > pageno + 4) {
pageEnd = pageno + 4;
}
else {
pageEnd = totalpage;
}
}
else if("".equals(pageno)) {
pageStart = 1;
if (totalpage > 8) {
pageEnd = 8;
}
else {
pageEnd = totalpage;
}
}else{
pageStart = 1;
if (totalpage > 8) {
pageEnd = 8;
}
else {
pageEnd = totalpage;
}
}
out.println("<table width=\"80%\" align=\"center\" cellPadding=\"0\" cellSpacing=\"0\" class=\"MyTableHeader\"> ");
out.print("<tr >");
out.println("<td width=47%>共" + totalcount + "條,," + totalpage + "頁,,當前是第"
+ pageno + "頁</td>");
out.println("<td width=53% align=‘left‘> ");
if (pageno > 1) {
out.println("<a href=\"" + action
+ "&pageno=1" + "&totalpage=" + totalpage + "&totalcount="
+ totalcount + "\">");
}
out.print("首頁");
out.println("</a> ");
if (pageno > 1) {
out.println("<a href=\"" + action + "&pageno="
+ minpageno + "&totalpage=" + totalpage + "&totalcount="
+ totalcount + "\">");
}
out.print("上頁");
out.println("</a> ");
//顯示頁數(shù)序列,。Ex:1 2,,3,4,、,、、,、
for (int i = pageStart; i < pageEnd+1; i++) {
out.println("<a href=\"" + action + "&pageno="
+ i + "&totalpage=" + totalpage + "&totalcount="
+ totalcount + "\">");
out.print("<strong>");
out.print(i);
out.print("</strong>");
out.println("</a> ");
}
if (pageno < totalpage) {
out.println("<a href=\"" + action + "&pageno="
+ addpageno + "&totalpage=" + totalpage + "&totalcount="
+ totalcount + "\">");
}
out.print("下頁");
out.println("</a> ");
if (pageno < totalpage) {
out.println("<a href=\"" + action + "&pageno="
+ totalpage + "&totalpage=" + totalpage + "&totalcount="
+ totalcount + "\">");
}
out.print("末頁");
out.println("</a> ");
out.println("</td> </tr> </table>");
}
}
編寫.tld描述文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated by Taglib Descriptor Wizard -->
<taglib version="2.0" xmlns="http://java./xml/ns/j2ee" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://java./xml/ns/j2ee
http://java./xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
<tlib-version>1.0</tlib-version>
<short-name>MyTag</short-name>
<tag>
<name>page</name>
<tag-class>com.myecsun.util.helper.PageTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>page</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>action</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>nodeID</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
在Action中使用:
int total = groupService.getCount();
int page = pageHelper.getPageInfo(request, total).getPageno();
int pageSize = pageHelper.getPageInfo(request, total).getPagesize();
FastArrayList groups=new FastArrayList(groupService.getPosts(pageSize,page).getResultsList()); request.setAttribute("groups",groups);
request.setAttribute("page", pageHelper.getPageInfo(request, total));
return mapping.findForward("queryGroup");
在web.xml中配置taglib:
<taglib>
<taglib-uri>/page</taglib-uri>
<taglib-location>/WEB-INF/mytld/page.tld</taglib-location>
</taglib>
在網(wǎng)頁中加入標簽:
<%@taglib uri="/page" prefix="page"%>
<%@page import="com.myecsun.util.helper.OldPage"%>
<page:page page="<%=(OldPage) request.getAttribute("page")%>" action="queryAction.html?method=queryGroup"/>