大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
package common.util;
网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、微信平台小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了七星免费建站欢迎大家使用!
import java.util.*;
public class PageController implements IPageModel {
private Collection model;
//数据总行数
private int totalRowCount = 0; //
//总页数
private int pageCount = 0;
//每页应显示的行数
private int maxPageRowCount = 0;
//当前页行数
private int currPageRowCount = 0;
//当前页号
private int currPageNum;
//默认构造
public PageController() {
super();
}
//传入模型
public PageController(Collection model) {
setPageController(model);
}
//设一个分页模型
public void setPageController(Collection model) {
this.model = model;
this.totalRowCount = model.size();
}
/**
* 总页数
* @return int
*/
public int getPageCount() {
return this.pageCount;
}
/**
* getPageContents
*
* @param intPageNum int
* @return Object
*/
public Object getPageContents(int intPageNum) {
//非法数据
if(intPageNum1){
intPageNum=1;
}
if(intPageNumpageCount){
intPageNum=pageCount;
}
//指定当前页
this.currPageNum=intPageNum;
int i = 0;
ArrayList arr = new ArrayList();
//如果是合法的范围
if (intPageNum 0 intPageNum = pageCount) {
//计算该页的开始号和结束号
int lfromrow = (intPageNum - 1) * maxPageRowCount;
arr = (ArrayList) getElementsAt(model, lfromrow, lfromrow + maxPageRowCount-1);
}
currPageNum=intPageNum;
return arr;
}
public Object getLastPage() {
return this.getPageContents(pageCount);
}
public Object getFirstPage() {
return this.getPageContents(0);
}
/**
* getCurrentPageRowsCount
*
* @return int
*/
public int getCurrentPageRowsCount() {
if(currPageNumpageCount){
return maxPageRowCount;
}
else{//最后一页
return totalRowCount-(pageCount-1)*maxPageRowCount;
}
}
public int getCurrentPageNum(){
return currPageNum;
}
/**
* setMaxPageRows
*
* @return int
*/
public void setMaxPageRows(int rowCount) {
maxPageRowCount = rowCount;
//计算总页数
if (totalRowCount % maxPageRowCount 0) { //有余数
pageCount = totalRowCount / maxPageRowCount + 1;
}
else {
pageCount = totalRowCount / maxPageRowCount;
}
}
/**
* getMaxPageRows
*/
public int getMaxPageRows() {
return maxPageRowCount;
}
//私有方法,返回集合中指定范围的数据
private Object getElementsAt(Collection model, int fromIndex, int toIndex) {
Iterator iter = model.iterator();
ArrayList arr = new ArrayList();
if (iter != null) {
int i = 0;
while (iter.hasNext()) {
Object obj=iter.next();
if (i = fromIndex i = toIndex) {
arr.add(obj);
}
if (i toIndex) {
break;
}
i = i + 1;
}
}
return arr;
}
}
Pager类:
package bit.dlde.marx.util;
import java.math.*;
public class NewPager {
private int totalRows; //总行数
private int pageSize = 10; //每页显示的行数
private int currentPage; //当前页号
private int totalPages; //总页数
private int startRow; //当前页在数据库中的起始行
public NewPager() {
}
/**默认每页10个
* @param _totalRows
*/
public NewPager(int _totalRows) {
totalRows = _totalRows;
totalPages=totalRows/pageSize;
// System.out.println("总共的页数:"+totalPages);
int mod=totalRows%pageSize;
if(mod0){
totalPages++;
}
currentPage = 1;
startRow = 0;
}
/**可自定义每页显示多少数
* @param _totalRows
* @param _pageSize
*/
public NewPager(int _totalRows, int _pageSize) {
totalRows = _totalRows;
pageSize = _pageSize;
totalPages = totalRows / pageSize;
// System.out.println("总共的页数:"+totalPages);
int mod = totalRows % pageSize;
if (mod 0) {
totalPages++;
}
currentPage = 1;
startRow = 0;
}
public void first() {
currentPage = 1;
startRow = 0;
}
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
}
public void next() {
if (currentPage totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
}
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
}
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage totalPages) {
last();
}
}
public int getStartRow() {
return startRow;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRows() {
return totalRows;
}
}
PageService 类:
package bit.dlde.marx.util;
public class NewPagerService {
/**
* 功能描述:使用默认参数的
* br输入:
* br输出:
* br创建日期:Nov 6, 2008
*/
public NewPager getPager(String currentPage,String pagerMethod,int totalRows) {
// 定义pager对象,用于传到页面
NewPager pager = new NewPager(totalRows);
// System.out.println("执行到getPager了");
// 如果当前页号为空,表示为首次查询该页
// 如果不为空,则刷新pager对象,输入当前页号等信息
if (currentPage != null) {
pager.refresh(Integer.parseInt(currentPage));
// System.out.println("确认完当前的currentPage");
}else{
// currentPage="1";
pager.refresh(1);
}
// 获取当前执行的方法,首页,前一页,后一页,尾页。
// System.out.println("当前要执行的页面动作为:"+pagerMethod);
if (pagerMethod != null) {
if (pagerMethod.equals("first")) {
pager.first();
} else if (pagerMethod.equals("previous")) {
pager.previous();
} else if (pagerMethod.equals("next")) {
pager.next();
} else if (pagerMethod.equals("last")) {
pager.last();
}
}else{
pager.first();
}
return pager;
}
/**
* 功能描述:可自定义每页显示书目的
* br输入:
* br输出:
* br创建日期:Nov 6, 2008
*/
public NewPager getPager(String currentPage,String pagerMethod,int totalRows,int pageSize) {
// 定义pager对象,用于传到页面
NewPager pager = new NewPager(totalRows,pageSize);
// System.out.println("执行到getPager了");
// 如果当前页号为空,表示为首次查询该页
// 如果不为空,则刷新pager对象,输入当前页号等信息
if (currentPage != null) {
pager.refresh(Integer.parseInt(currentPage));
// System.out.println("确认完当前的currentPage");
}else{
// currentPage="1";
pager.refresh(1);
}
// 获取当前执行的方法,首页,前一页,后一页,尾页。
// System.out.println("当前要执行的页面动作为:"+pagerMethod);
if (pagerMethod != null) {
if (pagerMethod.equals("first")) {
pager.first();
} else if (pagerMethod.equals("previous")) {
pager.previous();
} else if (pagerMethod.equals("next")) {
pager.next();
} else if (pagerMethod.equals("last")) {
pager.last();
}
}else{
pager.first();
}
return pager;
}
}
调用示例:
/**
* 功能描述:前台列表
* br输入:
* br输出:
* br创建日期:Nov 6, 2008
*/
public String ExpListForFront() throws Exception {
int totalRow = expServ.getTotalRowsNum();
System.out.println(totalRow+" :totalRow");
pager = pagerService.getPager(this.getCurrentPage(), this
.getPagerMethod(), totalRow,36);//前台页面一页36个
this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
this.setTotalRows(String.valueOf(totalRow));
availableItems = expServ.getRows(pager.getPageSize(), pager.getStartRow());
System.out.println("准备显示专家列表");
ListExperts explistemp = new ArrayListExperts();
for (Experts es : availableItems) {
es.setName(CharacterTrans.trans8859_1ToGBK(es.getName()));
es.setResume(CharacterTrans.trans8859_1ToGBK(es.getResume()));
es.setPhoto(CharacterTrans.trans8859_1ToGBK(es.getPhoto()));
es.setEditor(CharacterTrans.trans8859_1ToGBK(es.getEditor()));
es.getExpertType().setTempname(CharacterTrans.trans8859_1ToGBK(es.getExpertType().getName()));
explistemp.add(es);
}
this.setItEx(explistemp.iterator());
System.out.println("显示专家列表完毕");
return SUCCESS;
}
页面上设置:
div id="pageflip"
共
s:property value="totalRows" /
行 第
s:property value="currentPage" /
页 共
s:property value="pager.getTotalPages()" /
页
a
href="s:url value="expListForFront.action"
s:param name="currentPage" value="currentPage"/
s:param name="pagerMethod" value="'first'"/
/s:url"首页/a
a
href="s:url value="expListForFront.action"
s:param name="currentPage" value="currentPage"/
s:param name="pagerMethod" value="'previous'"/
/s:url"上一页/a
a
href="s:url value="expListForFront.action"
s:param name="currentPage" value="currentPage"/
s:param name="pagerMethod" value="'next'"/
/s:url"下一页/a
a
href="s:url value="expListForFront.action"
s:param name="currentPage" value="currentPage"/
s:param name="pagerMethod" value="'last'"/
/s:url"尾页/a
/div
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import com.lqh.dao.db.DBCon;
public class PageDAO {
public static final String Text = "text";
public static final String Image = "image";
public static final String BbsText = "bbstext";
public static final String BbsImage = "bbsimage";
private HttpServletRequest request;
private int currentpage = 1; // 当前是第几页
private int pagecount = 0; // 一共有多少页
private int rscount = 0; // 一共有多少行
private int pagesize = 10; // 每页有多少行[默认为20行]
public PageDAO(HttpServletRequest request) {
this.request = request;
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
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 getRscount() {
return rscount;
}
public void setRscount(int rscount) {
this.rscount = rscount;
}
/**
* 传入SQL语句获取总记录数
*/
public int getRsCountForRs(String sql) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
DBCon dbcon=new DBCon();
try {
conn = dbcon.getConn();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next()) {
rs.last();
this.rscount = rs.getRow();
} else {
this.rscount = 0;
}
} catch (Exception ex) {
ex.printStackTrace();
this.rscount = 0;
} finally {
dbcon.tryClose(rs, ps, conn);
}
return this.rscount;
}
public int getRsCountForSQL(String sql) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
DBCon dbcon=new DBCon();
try {
conn = dbcon.getConn();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next()) {
this.rscount = rs.getInt("rscount");
} else {
this.rscount = 0;
}
} catch (Exception ex) {
ex.printStackTrace();
this.rscount = 0;
} finally {
dbcon.tryClose(rs, ps, conn);
}
return this.rscount;
}
/**
* 获取总页数
*
* @return int
*/
public int getPageCount() {
try {
this.pagecount = ((this.rscount - 1) / this.pagesize) + 1;
} catch (Exception ex) {
this.pagecount = 0;
}
return this.pagecount;
}
/**
* 获取当前页码的设置
*
* @return int
*/
public int getCurrentPage() {
try {
if (this.request.getParameter("currentpage") != null
Integer.parseInt(this.request
.getParameter("currentpage")) 1) {
this.currentpage = Integer.parseInt(this.request
.getParameter("currentpage"));
} else {
this.currentpage = 1;
}
} catch (Exception ex) {
this.currentpage = 1;
}
return this.currentpage;
}
/**
* 分页工具条
*
* @param fileName
* String
* @return String
*/
public String pagetool(String flag) {
StringBuffer str = new StringBuffer();
String url = this.getParamUrl();
int ProPage = this.currentpage - 1;
int Nextpage = this.currentpage + 1;
// 文字的分页
if (flag.equals(PageDAO.Text)) {
str.append("form method='post' name='pageform' action=''");
str
.append("table style='color: windowframe' width='100%' border='0' cellspacing='0' cellpadding='0'");
str.append("tr");
str.append("td width='20%'/td");
str.append("td height='26'");
str.append("共有记录" + this.rscount + "条 ");
str.append("共" + this.pagecount + "页 ");
str.append("每页" + this.pagesize + "记录 ");
str.append("现在" + this.currentpage + "/" + this.pagecount + "页");
str.append("/tdtd");
if (this.currentpage 1) {
str.append("a href='" + url + "currentpage=1'首页/a");
str.append(" ");
str.append("a href='" + url + "currentpage=" + ProPage
+ "'上一页/a");
str.append(" ");
} else {
str.append("首页");
str.append(" ");
str.append("上一页");
str.append(" ");
}
if (this.currentpage this.pagecount) {
str.append("a href='" + url + "currentpage=" + Nextpage
+ "'下一页/a");
str.append(" ");
} else {
str.append("下一页");
str.append(" ");
}
if (this.pagecount 1 this.currentpage != this.pagecount) {
str.append("a href='" + url + "currentpage=" + pagecount
+ "'尾页/a");
str.append(" ");
} else {
str.append("尾页");
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页");
str.append("/tdtd width='3%' /td/tr/table");
str.append("script language='javascript'");
str.append("function ChangePage(testpage){");
str.append("document.pageform.action='" + url
+ "currentpage='+testpage+'';");
str.append("document.pageform.submit();");
str.append("}");
str.append("/script");
str.append("/form");
} else if (flag.equals(PageDAO.Image)) {
/**
* 图片的分页
*/
} else if (flag.equals(PageDAO.BbsText)) {
/**
* 论坛形式的分页[直接以数字方式体现]
*/
str
.append("table width='100%' border='0' cellspacing='0' cellpadding='0'");
str.append("tr");
str.append("td width='3%' /td");
str.append("td height='26'");
str.append("记录" + this.rscount + "条 ");
str.append("共" + this.pagecount + "页 ");
str.append("每页" + this.pagesize + "记录 ");
str.append("现在" + this.currentpage + "/" + this.pagecount + "页");
str.append("/tdtd");
// 设定是否有首页的链接
if (this.currentpage 1) {
str.append("a href='" + url + "currentpage=1'首页/a");
str.append(" ");
}
// 设定是否有上一页的链接
if (this.currentpage 1) {
str.append("a href='" + url + "currentpage=" + ProPage
+ "'上一页/a");
str.append(" ");
}
// 如果总页数只有10的话
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 + "currentpage=" + i
+ "'" + i + "/a ");
}
}
} else {
// 说明总数有超过10页
// 制定特环的开始页和结束页
int 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 {
// 表示从第一页开始算
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 + "currentpage=" + i
+ "'" + i + "/a ");
}
}
}
// 设定是否有下一页的链接
if (this.currentpage this.pagecount) {
str.append("a href='" + url + "currentpage=" + Nextpage
+ "'下一页/a");
str.append(" ");
}
// 设定是否有尾页的链接
if (this.pagecount 1 this.currentpage != this.pagecount) {
str.append("a href='" + url + "currentpage=" + pagecount
+ "'尾页/a");
str.append(" ");
}
str.append("/tdtd width='3%' /td/tr/table");
} else if (flag.equals(PageDAO.BbsImage)) {
/**
* 论坛形式的分页[以图片的方式体现]
*/
// 设定分页显示的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('"
+ this.request.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\"");
// 判定是否有上一页
if (this.currentpage 1) {
str.append("a href='" + url
+ "currentpage=1' hidefocus=\"true\"首页/a");
str.append(" ");
str.append("a href='" + url + "currentpage=" + ProPage
+ "' hidefocus=\"true\"上一页/a");
str.append(" ");
} else {
str.append("span class=\"disabled\"首页/span");
str.append(" ");
str.append("span class=\"disabled\"上一页/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 + "currentpage=" + i
+ "' hidefocus=\"true\"" + i
+ "/a ");
}
}
} else {
// 说明总数有超过10页
// 制定特环的开始页和结束页
int 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 {
// 表示从第一页开始算
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 + "currentpage=" + i
+ "' hidefocus=\"true\"" + i
+ "/a ");
}
}
}
// 判断下一页和尾页
if (this.currentpage this.pagecount) {
if (this.currentpage this.pagecount - 10) {
str.append("...");
str.append("a href='" + url + "currentpage="
+ (this.pagecount - 1) + "' hidefocus=\"true\""
+ (this.pagecount - 1) + "/a ");
str.append("a href='" + url + "currentpage="
+ this.pagecount + "' hidefocus=\"true\""
+ this.pagecount + "/a ");
}
str.append("a href='" + url + "currentpage=" + Nextpage
+ "' hidefocus=\"true\"下一页/a");
str.append(" ");
} else {
str.append("span class=\"disabled\"下一页/span");
str.append(" ");
}
if (this.pagecount 1 this.currentpage != this.pagecount) {
str.append("a href='" + url + "currentpage=" + pagecount
+ "' hidefocus=\"true\"尾页/a");
str.append(" ");
} else {
str.append("span class=\"disabled\"尾页/span");
str.append(" ");
}
str.append("/div");
}
return str.toString();
}
public String getParamUrl() {
String url = "";
url = this.request.getRequestURI().toString();
if (url.indexOf("?") == -1) {
url = url + "?";
}
String totalParams = "";
Enumeration params = this.request.getParameterNames();// 得到所有参数名
while (params.hasMoreElements()) {
String tempName = params.nextElement().toString();
String tempValue = this.request.getParameter(tempName);
if (tempValue != null !tempValue.equals("")
!tempName.equals("currentpage")) {
if (totalParams.equals("")) {
totalParams = totalParams + tempName + "=" + tempValue;
} else {
totalParams = totalParams + "" + tempName + "="
+ tempValue;
}
}
}
String totalUrl = url + totalParams;
return totalUrl;
}
}
以下是最简单的分页(一个方法搞定):
首先是方法:
//用于按照页数和每页显示的记录数进行图书查询
public List getBookList(int page,int pageSize)throws Exception{
List list=new ArrayList();
Connection conn=this.getConn();
String sql="select top "+page+" * from bookInfo " +
"where bookId not in" +
"(select top "+((pageSize-1)*page)+" bookId from bookInfo)";
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
//每条数据对应一个实体类
BookInfo book=new BookInfo();
book.setBookId(rs.getInt("bookId"));
book.setBookName(rs.getString("bookName"));
book.setBookType(rs.getString("bookType"));
book.setBookStatus(rs.getString("bookStatus"));
list.add(book);
}
this.closeAll(conn, pstmt, rs);
return list;
}
2:JSP页面代码:
%@ page language="java" import="java.util.*,entity.*,operator.*" pageEncoding="gbk"%
html
head
/head
body
table border="1"
tr
td width="150"图书名称/td
td width="100"类别/td
td width="100"状态/td
/tr
%
request.setCharacterEncoding("gbk");
int pageNum=1;
String pageStr=request.getParameter("pageNum");
if(pageStr==null){
pageNum=1;
}else{
pageNum=Integer.parseInt(pageStr);
}
Operator o=new Operator();
List list=o.getBookList(6,pageNum);
for(int i=0;ilist.size();i++){
BookInfo book=(BookInfo)list.get(i);
%
tr
td%=book.getBookName() %/td
td%=book.getBookType() %/td
td%=book.getBookStatus() %/td
/tr
%
}
%
tr
td colspan="3"a href="infoList.jsp?pageNum=%=pageNum==1?pageNum:pageNum-1%"上一页/a ||
a href="infoList.jsp?pageNum=%=list.size()6?pageNum:pageNum+1 %"下一页/a/td//二元运算符,相当于if判断 上一页时如果当前页=1为true,就是当前页,否则还可以减1
//下一页如果当前页的size小于你的每一页设置的条数为true,那么就是是当前页,否则就+1;
/tr
/table
/body
/html
下面的代码是纯jsp页面分页
也有java后台代码的分页,你如果想要的话就说。
%@ page contentType="text/html; charset=gb2312" %
%@ page language="java" %
%@ page import="java.sql.*" %
%
//驱动程序名,比较旧了,如果你用mysql5,自己改。
String driverName="org.gjt.mm.mysql.Driver";
String userName="root";//数据库用户名
String userPasswd="";//密码
String dbName="bookstore";//数据库名
String tableName="items"; //表名
//连接字符串
String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"password="+userPasswd;
Class.forName(driverName).newInstance();
Connection connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
//每页显示记录数
int PageSize = 8;
int StartRow = 0; //开始显示记录的编号
int PageNo=0;//需要显示的页数
int CounterStart=0;//每页页码的初始值
int CounterEnd=0;//显示页码的最大值
int RecordCount=0;//总记录数;
int MaxPage=0;//总页数
int PrevStart=0;//前一页
int NextPage=0;//下一页
int LastRec=0;
int LastStartRecord=0;//最后一页开始显示记录的编号
//获取需要显示的页数,由用户提交
if(request.getParameter("PageNo")==null){ //如果为空,则表示第1页
if(StartRow == 0){
PageNo = StartRow + 1; //设定为1
}
}else{
PageNo = Integer.parseInt(request.getParameter("PageNo")); //获得用户提交的页数
StartRow = (PageNo - 1) * PageSize; //获得开始显示的记录编号
}
//设置显示页码的初始值
if(PageNo % PageSize == 0){
CounterStart = PageNo - (PageSize - 1);
}else{
CounterStart = PageNo - (PageNo % PageSize) + 1;
}
CounterEnd = CounterStart + (PageSize - 1);
%
html
head
title分页显示记录/title
link rel="stylesheet" href="style.css" type="text/css"
/head
%
//获取总记录数
ResultSet rs = statement.executeQuery("select count(*) from items" );
rs.next();
RecordCount = rs.getInt(1);
rs = statement.executeQuery("SELECT image_url,author,price,item_id FROM items ORDER BY item_id DESC LIMIT "
+StartRow+", "+PageSize);
//获取总页数
MaxPage = RecordCount % PageSize;
if(RecordCount % PageSize == 0){
MaxPage = RecordCount / PageSize;
}else{
MaxPage = RecordCount/PageSize+1;
}
%
body class="UsePageBg"
table width="100%" border="0" class="InternalHeader"
tr
td width="24%"font size=4分页显示记录/font/td
td width="76%"
font size=4%="总共"+RecordCount+"条记录 - 当前页:"+PageNo+"/"+MaxPage %/font
/td
/tr
/table
br
table width="100%" border="0" class="NormalTableTwo"
tr
td class="InternalHeader"记录序号/td
td class="InternalHeader" 图像路径/td
td class="InternalHeader" 作者/td
td class="InternalHeader" 价格/td
td class="InternalHeader" 图书编号/td
/tr
%
int i = 1;
while (rs.next()) {
int bil = i + (PageNo-1)*PageSize;
%
tr
td class="NormalFieldTwo" %=bil %/td
td class="NormalFieldTwo" %=rs.getString(1)%/td
td class="NormalFieldTwo" %=rs.getString(2)%/td
td class="NormalFieldTwo" %=rs.getString(3)%/td
td class="NormalFieldTwo" %=rs.getString(4)%/td
/tr
%
i++;
}%
/table
br
table width="100%" border="0" class="InternalHeader"
tr
tddiv align="center"
%
out.print("font size=4");
//显示第一页或者前一页的链接
//如果当前页不是第1页,则显示第一页和前一页的链接
if(PageNo != 1){
PrevStart = PageNo - 1;
out.print("a href=TestPage.jsp?PageNo=1第一页 /a: ");
out.print("a href=TestPage.jsp?PageNo="+PrevStart+"前一页/a");
}
out.print("[");
//打印需要显示的页码
for(int c=CounterStart;c=CounterEnd;c++){
if(c MaxPage){
if(c == PageNo){
if(c %PageSize == 0){
out.print(c);
}else{
out.print(c+" ,");
}
}else if(c % PageSize == 0){
out.print("a href=TestPage.jsp?PageNo="+c+""+c+"/a");
}else{
out.print("a href=TestPage.jsp?PageNo="+c+""+c+"/a ,");
}
}else{
if(PageNo == MaxPage){
out.print(c);
break;
}else{
out.print("a href=TestPage.jsp?PageNo="+c+""+c+"/a");
break;
}
}
}
out.print("]");;
if(PageNo MaxPage){ //如果当前页不是最后一页,则显示下一页链接
NextPage = PageNo + 1;
out.print("a href=TestPage.jsp?PageNo="+NextPage+"下一页/a");
}
//同时如果当前页不是最后一页,要显示最后一页的链接
if(PageNo MaxPage){
LastRec = RecordCount % PageSize;
if(LastRec == 0){
LastStartRecord = RecordCount - PageSize;
}
else{
LastStartRecord = RecordCount - LastRec;
}
out.print(":");
out.print("a href=TestPage.jsp?PageNo="+MaxPage+"最后一页/a");
}
out.print("/font");
%
/div
/td
/tr
/table
%
rs.close();
statement.close();
connection.close();
%
/body
/html