1、在index.jsp中有<a href="<%=path%>/servlet/C_Dis?action=getAllList">移動(dòng)考勤</a>
2、在C_Dis的Servlet中頁(yè)面如下:
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
res.setCharacterEncoding("UTF-8");
req.setCharacterEncoding("UTF-8");
PrintWriter out = res.getWriter();
public AttendanceManage attm = AttendanceManage.getInterface();
String action =req.getParameter("action");
int intPageSize = 5; // 單頁(yè)顯示的記錄數
int intRowCount = 0; // 記錄總數
int intPageCount = 1; // 總頁(yè)數
int intPage = 1; // 待顯示頁(yè)碼
int currentpage = 1;
if(action.equals("getAllList"))
{
String currentpagetemp = HttpParamCaster.getUTF8Parameter(req,"currentpage");
if(currentpagetemp!=null && !currentpagetemp.equals(""))
{
currentpage=Integer.valueOf(currentpagetemp);
}
DispLbsAttendance dislbs = new DispLbsAttendance();
long rscount=attm.rscount(dislbs);
List<DispLbsAttendance> list=attm.getAllDisplbs(dislbs, currentpage);
req.setAttribute("list", list);
req.setAttribute("rscount",rscount);
req.setAttribute("currentpage",currentpage);
req.getRequestDispatcher("/attList.jsp").forward(req, res);
}
}
注意:一定要在首頁(yè)index.jsp中使用標示來(lái)判斷實(shí)行Servlet中的那個(gè)方法,否則分頁(yè)會(huì )出錯
3、為
AttendanceManage類(lèi)如下:
//獲得的列表
public List getAllDisplbs(DispLbsAttendance dislbs, int currentpage)
{
List list=null;
Session session=null;
session=HibernateUtil.getInstance().openSession();
StringBuffer hql = new StringBuffer("from DispLbsAttendance t where 1=1");
Query query=session.createQuery(hql.toString());
int pagesize = Contains.PAGE_SIZE;
query.setFirstResult(pagesize * (currentpage - 1));
query.setMaxResults(pagesize);
list=query.list();
return list;
}
//獲得總記錄數
public long rscount(DispLbsAttendance displbs)
{
Session session = null;
Transaction tx = null;
List list = null;
long rscount=0;
session=HibernateUtil.getInstance().openSession();
tx = session.beginTransaction();
StringBuffer hql = new StringBuffer(
"select count(*) from DispLbsAttendance where 1=1");
Query query=session.createQuery(hql.toString());
rscount=(Long)query.uniqueResult();
return rscount;
}
4、顯示列表頁(yè)面的分頁(yè):
導入標簽:<%@ taglib uri="/WEB-INF/tld_file/lywu.tld" prefix="lywu"%>
<div id="div_s" style="display: ; position: absolute; z-index: 1;">
<lywu:pagetools rscount="${rscount}" action="/servlet/C_Dis" currentpage="${currentpage}" className="bbstext"></lywu:pagetools>
</div>
5、自定義標簽lywu.tld:
<?xml version="1.0" encoding="UTF-8"?>
6、自定義標簽類(lèi):
package com.gps.util;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import com.cari.web.util.HttpParamCaster;
import com.gps.util.Contains;
/**
* 分頁(yè)標簽處理類(lèi)
*
* @author yuanwu
*
*/
public class PageTag extends TagSupport {
private long rscount; // 總記錄數
private int currentpage; // 當前頁(yè)數
private int pagesize = Contains.PAGE_SIZE; // 每頁(yè)記錄數
private long pagecount;
private String action;// 請求
private String className = Contains.TEXT;
public int doStartTag() throws JspException {
String pagetools = pagetool();
JspWriter out = this.pageContext.getOut();
try {
out.write(pagetools);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}
public String pagetool() {
StringBuffer str = new StringBuffer();
String temp = "";
String url = this.getParamUrl();
int ProPage = this.currentpage - 1;
int Nextpage = this.currentpage + 1;
// 文字的分頁(yè)
if (className.equals(Contains.TEXT)) {
str.append("<form method='post' name='pageform' action=''>");
str
.append("<table width='100%' border='0' cellspacing='0' cellpadding='0' style='color: #0099FF'>");
str.append("<tr>");
str.append("<td width='3%'> </td>");
str.append("<td height='26'>");
str.append("共有記錄" + this.rscount + "條 ");
str.append("共" + this.pagecount + "頁(yè) ");
str.append("每頁(yè)" + this.pagesize + "記錄 ");
str.append("現在" + this.currentpage + "/" + this.pagecount + "頁(yè)");
str.append("</td><td>");
if (this.currentpage > 1) {
str.append("<a href='" + url + "¤tpage=1'>首頁(yè)</a>");
str.append(" ");
str.append("<a href='" + url + "¤tpage=" + ProPage
+ "'>上一頁(yè)</a>");
str.append(" ");
} else {
str.append("首頁(yè)");
str.append(" ");
str.append("上一頁(yè)");
str.append(" ");
}
if (this.currentpage < this.pagecount) {
str.append("<a href='" + url + "¤tpage=" + Nextpage
+ "'>下一頁(yè)</a>");
str.append(" ");
} else {
str.append("下一頁(yè)");
str.append(" ");
}
if (this.pagecount > 1 && this.currentpage != this.pagecount) {
str.append("<a href='" + url + "¤tpage=" + pagecount
+ "'>尾頁(yè)</a>");
str.append(" ");
} else {
str.append("尾頁(yè)");
str.append(" ");
}
// str.append("轉到");
// str
// .append("<select name='currentpage'
// onchange='javascript:ChangePage(this.value);'>");
// for (int j = 1; j <= pagecount; j++) {
// str.append("<option value='" + j + "'");
// if (currentpage == j) {
// str.append("selected");
// }
// str.append(">");
// str.append("" + j + "");
// str.append("</option>");
// }
// str.append("</select>頁(yè)");
// str.append("</td><td width='3%'> </td></tr></table>");
// str.append("<script language='javascript'>");
// str.append("function ChangePage(testpage){");
// str.append("document.pageform.action='" + url
// + "¤tpage='+testpage+'';");
// str.append("document.pageform.submit();");
// str.append("}");
// str.append("</script>");
str.append("</form>");
} else if (className.equals(Contains.IMAGE)) {
/**
* 圖片的分頁(yè)
*/
} else if (className.equals(Contains.BBSTEXT)) {
/**
* 論壇形式的分頁(yè)[直接以數字方式體現]
*/
str
.append("<table width='100%' border='0' cellspacing='0' cellpadding='0' style='color: #0099FF'>");
str.append("<tr>");
str.append("<td width='3%'> </td>");
str.append("<td height='26'>");
str.append("記錄" + this.rscount + "條 ");
str.append("共" + this.pagecount + "頁(yè) ");
str.append("每頁(yè)" + this.pagesize + "記錄 ");
str.append("現在" + this.currentpage + "/" + this.pagecount + " 頁(yè)");
str.append("</td><td>");
// 設定是否有首頁(yè)的鏈接
if (this.currentpage > 1) {
str.append("<a href='" + url + "¤tpage=1'>首頁(yè)</a>");
str.append(" ");
}
// 設定是否有上一頁(yè)的鏈接
if (this.currentpage > 1) {
str.append("<a href='" + url + "¤tpage=" + ProPage
+ "'>上一頁(yè)</a>");
str.append(" ");
}
// 如果總頁(yè)數只有10的話(huà)
if (this.pagecount <= 10) {
for (int i = 1; i <= this.pagecount; i++) {
if (this.currentpage == i) {
str.append("<font color=red>[" + i
+ "]</font> ");
} else {
str.append("<a href='" + url + "¤tpage=" + i
+ "'>" + i + "</a> ");
}
}
} else {
// 說(shuō)明總數有超過(guò)10頁(yè)
// 制定特環(huán)的開(kāi)始頁(yè)和結束頁(yè)
long endPage = this.currentpage + 2;
if (endPage > this.pagecount) {
endPage = this.pagecount;
}
int startPage = 0;
if (this.pagecount >= 8 && this.currentpage >= 8) {
startPage = this.currentpage - 2;
} else {
// 表示從第一頁(yè)開(kāi)始算
startPage = 1;
}
// System.out.println(startPage);
// System.out.println(endPage);
for (int i = startPage; i <= endPage; i++) {
if (this.currentpage == i) {
str.append("<font color=red>[" + i
+ "]</font> ");
} else {
str.append("<a href='" + url + "¤tpage=" + i
+ "'>" + i + "</a> ");
}
}
}
// 設定是否有下一頁(yè)的鏈接
if (this.currentpage < this.pagecount) {
str.append("<a href='" + url + "¤tpage=" + Nextpage
+ "'>下一頁(yè)</a>");
str.append(" ");
}
// 設定是否有尾頁(yè)的鏈接
if (this.pagecount > 1 && this.currentpage != this.pagecount) {
str.append("<a href='" + url + "¤tpage=" + pagecount
+ "'>尾頁(yè)</a>");
str.append(" ");
}
str.append("轉到 ");
str
.append("<select name='currentpage' onchange='javascript:ChangePage(this.value);' size='0.5'>");
for (int j = 1; j <= pagecount; j++) {
str.append("<option value='" + j + "'");
if (currentpage == j) {
str.append("selected");
}
str.append(">");
str.append("" + j + "");
str.append("</option>");
}
str.append("</select>頁(yè)");
str.append("</td><td width='3%'> </td></tr></table>");
str.append("<script language='javascript'>");
str.append("function ChangePage(testpage){");
str.append("window.location.href='" + url
+ "¤tpage='+ testpage;");
str.append("}");
str.append("</script>");
str.append("</td><td width='3%'> </td></tr></table>");
} else if (className.equals(Contains.BBSIMAGE)) {
/**
* 論壇形式的分頁(yè)[以圖片的方式體現]
*/
// 設定分頁(yè)顯示的CSS
str.append("<style>");
str
.append("BODY {FONT-SIZE: 12px;FONT-FAMILY:宋體;WIDTH: 60%; PADDING-LEFT: 25px;}");
str
.append("DIV.meneame {PADDING-RIGHT: 3px; PADDING-LEFT: 3px; FONT-SIZE: 80%; PADDING-BOTTOM: 3px; MARGIN: 3px; COLOR: #ff6500; PADDING-TOP: 3px; TEXT-ALIGN: center}");
str
.append("DIV.meneame A {BORDER-RIGHT: #ff9600 1px solid; PADDING-RIGHT: 7px; BACKGROUND-POSITION: 50% bottom; BORDER-TOP: #ff9600 1px solid; PADDING-LEFT: 7px; BACKGROUND-IMAGE: url('"
+ ((HttpServletRequest) this.pageContext
.getRequest()).getContextPath()
+ "/meneame.jpg'); PADDING-BOTTOM: 5px; BORDER-LEFT: #ff9600 1px solid; COLOR: #ff6500; MARGIN-RIGHT: 3px; PADDING-TOP: 5px; BORDER-BOTTOM: #ff9600 1px solid; TEXT-DECORATION: none}");
str
.append("DIV.meneame A:hover {BORDER-RIGHT: #ff9600 1px solid; BORDER-TOP: #ff9600 1px solid; BACKGROUND-IMAGE: none; BORDER-LEFT: #ff9600 1px solid; COLOR: #ff6500; BORDER-BOTTOM: #ff9600 1px solid; BACKGROUND-COLOR: #ffc794}");
str
.append("DIV.meneame SPAN.current {BORDER-RIGHT: #ff6500 1px solid; PADDING-RIGHT: 7px; BORDER-TOP: #ff6500 1px solid; PADDING-LEFT: 7px; FONT-WEIGHT: bold; PADDING-BOTTOM: 5px; BORDER-LEFT: #ff6500 1px solid; COLOR: #ff6500; MARGIN-RIGHT: 3px; PADDING-TOP: 5px; BORDER-BOTTOM: #ff6500 1px solid; BACKGROUND-COLOR: #ffbe94}");
str
.append("DIV.meneame SPAN.disabled {BORDER-RIGHT: #ffe3c6 1px solid; PADDING-RIGHT: 7px; BORDER-TOP: #ffe3c6 1px solid; PADDING-LEFT: 7px; PADDING-BOTTOM: 5px; BORDER-LEFT: #ffe3c6 1px solid; COLOR: #ffe3c6; MARGIN-RIGHT: 3px; PADDING-TOP: 5px; BORDER-BOTTOM: #ffe3c6 1px solid}");
str.append("</style>");
str.append("<div class=\"meneame\">");
// 判定是否有上一頁(yè)
if (this.currentpage > 1) {
str.append("<a href='" + url
+ "¤tpage=1' hidefocus=\"true\">首頁(yè)</a>");
str.append(" ");
str.append("<a href='" + url + "¤tpage=" + ProPage
+ "' hidefocus=\"true\">上一頁(yè)</a>");
str.append(" ");
} else {
str.append("<span class=\"disabled\">首頁(yè)</span>");
str.append(" ");
str.append("<span class=\"disabled\">上一頁(yè)</span>");
str.append(" ");
}
// 顯示中間的圖片
if (this.pagecount <= 10) {
for (int i = 1; i <= this.pagecount; i++) {
if (this.currentpage == i) {
str.append("<span class=\"current\">" + i + "</span>");
} else {
str.append("<a href='" + url + "¤tpage=" + i
+ "' hidefocus=\"true\">" + i
+ "</a> ");
}
}
} else {
// 說(shuō)明總數有超過(guò)10頁(yè)
// 制定特環(huán)的開(kāi)始頁(yè)和結束頁(yè)
long endPage = this.currentpage + 4;
if (endPage > this.pagecount) {
endPage = this.pagecount;
}
int startPage = 0;
if (this.pagecount >= 8 && this.currentpage >= 8) {
startPage = this.currentpage - 5;
} else {
// 表示從第一頁(yè)開(kāi)始算
startPage = 1;
}
System.out.println(startPage);
System.out.println(endPage);
for (int i = startPage; i <= endPage; i++) {
if (this.currentpage == i) {
str.append("<span class=\"current\">" + i + "</span>");
} else {
str.append("<a href='" + url + "¤tpage=" + i
+ "' hidefocus=\"true\">" + i
+ "</a> ");
}
}
}
// 判斷下一頁(yè)和尾頁(yè)
if (this.currentpage < this.pagecount) {
if (this.currentpage < this.pagecount - 10) {
str.append("...");
str.append("<a href='" + url + "¤tpage="
+ (this.pagecount - 1) + "' hidefocus=\"true\">"
+ (this.pagecount - 1) + "</a> ");
str.append("<a href='" + url + "¤tpage="
+ this.pagecount + "' hidefocus=\"true\">"
+ this.pagecount + "</a> ");
}
str.append("<a href='" + url + "¤tpage=" + Nextpage
+ "' hidefocus=\"true\">下一頁(yè)</a>");
str.append(" ");
} else {
str.append("<span class=\"disabled\">下一頁(yè)</span>");
str.append(" ");
}
if (this.pagecount > 1 && this.currentpage != this.pagecount) {
str.append("<a href='" + url + "¤tpage=" + pagecount
+ "' hidefocus=\"true\">尾頁(yè)</a>");
str.append(" ");
} else {
str.append("<span class=\"disabled\">尾頁(yè)</span>");
str.append(" ");
}
str.append("</div>");
}
return str.toString();
}
private String getParamUrl() {
String url = "";
HttpServletRequest request = (HttpServletRequest) this.pageContext
.getRequest();
url = request.getContextPath() + action;
if (url.indexOf("?") == -1) {
url = url + "?";
}
String totalParams = "";
Enumeration params = request.getParameterNames(); // 獲取所有參數名
while (params.hasMoreElements()) {
String tempName = params.nextElement().toString();
// String tempValue = request.getParameter(tempName);
// try {
// tempValue=new String(tempValue.getBytes(),"UTF-8");
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
String tempValue=HttpParamCaster.getUTF8Parameter(request, tempName);
try {
if(tempValue!=null && !"".equals(tempValue))
tempValue=java.net.URLEncoder.encode(tempValue,"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (tempValue != null && !tempValue.equals("")
&& !tempName.equals("currentpage")) {
if (totalParams.equals("")) {
totalParams = totalParams + tempName + "=" + tempValue;
} else {
totalParams = totalParams + "&" + tempName + "="
+ tempValue;
}
}
}
String totalUtl = url + totalParams;
return totalUtl;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public long getPagecount() {
return pagecount;
}
public void setPagecount(long pagecount) {
this.pagecount = pagecount;
}
public int getPagesize() {
return pagesize;
}
public void setPagesize(int pagesize) {
this.pagesize = pagesize;
}
public long getRscount() {
return rscount;
}
public void setRscount(long rscount) {
this.rscount = rscount;
try {
this.pagecount = ((this.rscount - 1) / this.pagesize) + 1;
} catch (Exception ex) {
this.pagecount = 0;
}
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}