Java代码
Java后台:
public class PageEntity {
private int page;//当前页码
private int total;//一共多少条
private int totalPage;//一共多少页
private int firstResult;//当前首记录
private int lastResult;//当前尾记录
private int pageSize = 10; //每页几条数据
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getFirstResult() {
return firstResult;
}
public void setFirstResult(int firstResult) {
this.firstResult = firstResult;
}
public int getLastResult() {
return lastResult;
}
public void setLastResult(int lastResult) {
this.lastResult = lastResult;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}
//这里注意page必须在firstResult之前计算
public PageEntity getPageEntity(HttpServletRequest request,GenericIface entityDao,String where) {
String strPage = request.getParameter("page") == null ? "1":request.getParameter("page");
int page = Integer.parseInt(strPage);//要查询的页数
int total = entityDao.findNumber(where);//当前数据库总记录数
int pageSize = 20;//每页要显示的记录
int firstResult = 0;//起始记录
int lastResult = 0;//末尾记录
int totalPage = 0;
if(total > 0){
totalPage = (total + pageSize -1)/pageSize;
if(page == -1){
page = totalPage;
}else if(page < 1){
page = 1;
}else if(page > totalPage){
page = totalPage;
}
firstResult = (page-1)*pageSize;
lastResult = firstResult + pageSize;
}else{
//如果没有记录 则清零 因为页面有加1处理 所以firstResult=-1
page = 0;
firstResult = -1;
lastResult = 0;
}
PageEntity pageEntity = new PageEntity();
pageEntity.setFirstResult(firstResult);
pageEntity.setLastResult(lastResult);
pageEntity.setPage(page);
pageEntity.setTotal(total);
pageEntity.setTotalPage(totalPage);
pageEntity.setPageSize(pageSize);
return pageEntity;
}
前台js:
//分页程序 1表示第一页 2表示上一页 3表示下一页 4表示尾页
function changePage(e){
var page = document.getElementById("nowPage") == null ? "1":document.getElementById("nowPage").value;
switch(e){
case 1:page=1;break;
case 2:page--;break;
case 3:page++;break;
case 4:page=-1;break;
}
var url = "action.do?page=" + page + getCondition(page);
document.location.href = url;
}
function forwardPage(){
var page = document.getElementById("nowPage") == null ? "1":document.getElementById("nowPage").value;
var exp = /^(0|[1-9]\d*)$/;
var result = page.match(exp);
if(result != null){
var url = " action.do?page=" + page + getCondition(page);
document.location.href = url;
}else{
alert("请输入合法数字!");
}
}
Jsp页面:
<div id="page" class="w_872 mrg_s10">
<label><input name="" type="button" class="tnleft1" onClick="changePage(1)" title="首页" /></label>
<label><input name="" type="button" class="tnleft2" onClick="changePage(2)" title="上一页" /></label>
<label class="fo">页码 <input type="text" class="input5" id="nowPage" value="${pageEntity.page }" onblur="forwardPage()"> of <span>${pageEntity.totalPage }</span></label>
<label><input name="" type="button" class="tnrig2" onClick="changePage(3)" title="下一页" /></label>
<label><input name="" type="button" class="tnrig1" onClick="changePage(4)" title="尾页" /></label>
<label class="rig"><span>${pageEntity.firstResult+1 }</span> to <span>${pageEntity.lastResult }</span>,共<span>${pageEntity.total }</span>条</label>
</div>
页面效果展示:
文章出处 :http://java.chinaitlab.com/ServletJsp/816055.html