当前位置: 移动技术网 > IT编程>开发语言>Java > JSP通用高大上分页代码(超管用)

JSP通用高大上分页代码(超管用)

2017年12月08日  | 移动技术网IT编程  | 我要评论

先给大家展示下分页效果,如果亲们还很满意请参考以下代码。


在超链接中要保留参数

当使用多条件查询后,然后在点击第2 页时,这个第2页超链接没有条件了,所以会丢失条件,所以我们需要在页面上的所有链接都要保留条件!

我们要把条件以一个字符串的形式保存到pagebean的url中!这个任务交给servlet!

pagebean

package cn.itcast.cstm.domain;
import java.util.list;
public class pagebean<t> {
private int pc;// 当前页码page code
//private int tp;// 总页数total page
private int tr;// 总记录数total record
private int ps;// 每页记录数page size
private list<t> beanlist;// 当前页的记录
private string url;//它就是url后的条件!
public string geturl() {
return url;
}
public void seturl(string url) {
this.url = url;
}
public int getpc() {
return pc;
}
public void setpc(int pc) {
this.pc = pc;
}
/**
* 计算总页数
* @return
*/
public int gettp() {
// 通过总记录数和每页记录数来计算总页数
int tp = tr / ps;
return tr%ps==0 ? tp : tp+1;
}
// public void settp(int tp) {
// this.tp = tp;
// }
public int gettr() {
return tr;
}
public void settr(int tr) {
this.tr = tr;
}
public int getps() {
return ps;
}
public void setps(int ps) {
this.ps = ps;
}
public list<t> getbeanlist() {
return beanlist;
}
public void setbeanlist(list<t> beanlist) {
this.beanlist = beanlist;
}
}

jsp页面

第${pb.pc }页/共${pb.tp }页
<a href="${pb.url }&pc=1">首页</a>
<c:if test="${pb.pc > 1 }">
<a href="${pb.url }&pc=${pb.pc-1}">上一页</a>
</c:if>
<%-- 计算begin、end --%>
<c:choose>
<%-- 如果总页数不足10页,那么把所有的页数都显示出来! --%>
<c:when test="${pb.tp <= 10 }">
<c:set var="begin" value="1" />
<c:set var="end" value="${pb.tp }" />
</c:when>
<c:otherwise>
<%-- 当总页数>10时,通过公式计算出begin和end --%>
<c:set var="begin" value="${pb.pc-5 }" />
<c:set var="end" value="${pb.pc+4 }" /> 
<%-- 头溢出 --%>
<c:if test="${begin < 1 }">
<c:set var="begin" value="1" />
<c:set var="end" value="10" />
</c:if> 
<%-- 尾溢出 --%>
<c:if test="${end > pb.tp }">
<c:set var="begin" value="${pb.tp - 9 }" />
<c:set var="end" value="${pb.tp }" />
</c:if> 
</c:otherwise>
</c:choose>
<%-- 循环遍历页码列表 --%>
<c:foreach var="i" begin="${begin }" end="${end }">
<c:choose>
<c:when test="${i eq pb.pc }">
[${i }]
</c:when>
<c:otherwise>
<a href="${pb.url }&pc=${i}">[${i }]</a> 
</c:otherwise>
</c:choose>
</c:foreach>
<c:if test="${pb.pc < pb.tp }">
<a href="${pb.url }&pc=${pb.pc+1}">下一页</a>
</c:if>
<a href="${pb.url }&pc=${pb.tp}">尾页</a>

servlet

package cn.itcast.cstm.web.servlet;
import java.io.ioexception;
import java.io.unsupportedencodingexception;
import java.util.list;
import javax.servlet.servletexception;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import cn.itcast.commons.commonutils;
import cn.itcast.cstm.domain.customer;
import cn.itcast.cstm.domain.pagebean;
import cn.itcast.cstm.service.customerservice;
import cn.itcast.servlet.baseservlet;
public class customerservlet extends baseservlet {
private customerservice customerservice = new customerservice();
/**
* 获取pc
* 
* @param request
* @return
*/
private int getpc(httpservletrequest request) {
/*
* 1. 得到pc 如果pc参数不存在,说明pc=1 如果pc参数存在,需要转换成int类型即可
*/
string value = request.getparameter("pc");
if (value == null || value.trim().isempty()) {
return 1;
}
return integer.parseint(value);
}
//分页servlet
public string query(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
// system.out.println(geturl(request));
/*
* 0. 把条件封装到customer对象中 1. 得到pc 2. 给定ps 3.
* 使用pc和ps,以及条件对象,调用service方法得到pagebean 4. 把pagebean保存到request域中 5.
* 转发到list.jsp
*/
// 获取查询条件
customer criteria = commonutils.tobean(request.getparametermap(), customer.class);
/*
* 处理get请求方式编码问题!
*/
criteria = encoding(criteria);
int pc = getpc(request);// 得到pc
int ps = 10;// 给定ps的值,第页10行记录
pagebean<customer> pb = customerservice.query(criteria, pc, ps);
// 得到url,保存到pb中
pb.seturl(geturl(request));
request.setattribute("pb", pb);
return "f:/list.jsp";
}
/**
* 处理四样
* 
* @param criteria
* @return
* @throws unsupportedencodingexception
*/
private customer encoding(customer criteria) throws unsupportedencodingexception {
string cname = criteria.getcname();
string gender = criteria.getgender();
string cellphone = criteria.getcellphone();
string email = criteria.getemail();
if (cname != null && !cname.trim().isempty()) {
cname = new string(cname.getbytes("iso-8859-1"), "utf-8");
criteria.setcname(cname);
}
if (gender != null && !gender.trim().isempty()) {
gender = new string(gender.getbytes("iso-8859-1"), "utf-8");
criteria.setgender(gender);
}
if (cellphone != null && !cellphone.trim().isempty()) {
cellphone = new string(cellphone.getbytes("iso-8859-1"), "utf-8");
criteria.setcellphone(cellphone);
}
if (email != null && !email.trim().isempty()) {
email = new string(email.getbytes("iso-8859-1"), "utf-8");
criteria.setemail(email);
}
return criteria;
}
/**
* 截取url /项目名/servlet路径?参数字符串
* 
* @param request
* @return
*/
private string geturl(httpservletrequest request) {
string contextpath = request.getcontextpath();// 获取项目名
string servletpath = request.getservletpath();// 获取servletpath,即/customerservlet
string querystring = request.getquerystring();// 获取问号之后的参数部份
// 判断参数部份中是否包含pc这个参数,如果包含,需要截取下去,不要这一部份。
if (querystring.contains("&pc=")) {
int index = querystring.lastindexof("&pc=");
querystring = querystring.substring(0, index);
}
return contextpath + servletpath + "?" + querystring;
}
}

以上内容是是小编给大家分享的jsp通用高大上分页代码(超管用),希望对大家有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网