看看JSP中怎样实现分页显示
展开全部
分页依据:
select 字段列表 from 表名 limit m,n;
m: 表示起始记录,并且从0开始
n: 查询记录的个数,每页记录数
分页信息
共多少页
有没有上一页
有没有下一页
当前页
注:分页信息类Page
注2:创建分页信息辅助类PageUtil
public static Page createPage(int everyPage,int totalCount,int currentPage)
everyPage: 程序员定
totalCount: 总记录数,查询数据库表记录 select count(*) from 表名
currentPage: 从默认第一页开始,下一页= 当前页+1 上一页 = 当前页-1
分页数据集合List
依据查询语句获得集合: select 字段列表 from 表名 limit m,n;
m: beginIndex
n: everyPage
具体实现:::
UserBiz:
//分页
public int getCont();
public List<User> findByPage(Page page);
123
UserBizImpl:
@Override
public int getCont() {
String sql = "select count(*) as count from user";
CountUtil count = (CountUtil) udao.get(sql, CountUtil.class);
return count.getCount();
}
@Override
public List<User> findByPage(Page page) {
String sql = "select * from user limit "+page.getBeginIndex()+", "+page.getEveryPage();
return udao.query(sql, User.class);
}
servlet::UserServlet
int everyPage = 5;//每页记录数
int totalCount = ubiz.getCont();//获取总记录数
//点击链接重新获取当前页
String scurrentPage = request.getParameter("currentPage");
int currentPage = 1; //当前页,默认1
if(scurrentPage == null){
currentPage = 1;//从第一页开始访问
}else{
currentPage = Integer.parseInt(scurrentPage);
}
//分页信息
Page page = PageUtil.createPage(everyPage, totalCount, currentPage);
//分页数据信息
List<User> list = ubiz.findByPage(page);
request.setAttribute("page", page);
request.setAttribute("list", list);
//转发到userlist.jsp
request.getRequestDispatcher("/back/manager/userlist.jsp").forward(request, response);
userlist.jsp中的分页表现
<table width="461" height="24" border="1" cellpadding="0" cellspacing="0">
<tr>
<td width="199">当前为第${page.currentPage}页,共${page.totalPage}页</td>
<td width="256">
<c:choose>
<c:when test="${page.hasPrePage}">
<a href="<%=path %>/user.do?method=list¤tPage=1">首页</a> |
<a href="<%=path %>/user.do?method=list¤tPage=${page.currentPage -1 }">上一页</a>
</c:when>
<c:otherwise>
首页 | 上一页
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page.hasNextPage}">
<a href="<%=path %>/user.do?method=list¤tPage=${page.currentPage + 1 }">下一页</a> |
<a href="<%=path %>/user.do?method=list¤tPage=${page.totalPage }">尾页</a>
</c:when>
<c:otherwise>
下一页 | 尾页
</c:otherwise>
</c:choose>
</td>
</tr>
</table>
附::::page类(分页信息实体类)
public class Page {
private int everyPage; //每页显示记录数
private int totalCount; //总记录数
private int totalPage; //总页数
private int currentPage; //当前页
private int beginIndex; //查询起始点
private boolean hasPrePage; //是否有上一页
private boolean hasNextPage; //是否有下一页
public Page(int everyPage, int totalCount, int totalPage,
int currentPage,int beginIndex, boolean hasPrePage,
boolean hasNextPage) { //自定义构造方法
this.everyPage = everyPage;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
}
///////get,set方法略
}123456789101112131415161718192021
附:pageutil 分页信息辅助类
public class PageUtil {
//创建Page对象
public static Page createPage(int everyPage,int totalCount,int currentPage) {//创建分页信息对象
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int totalPage = getTotalPage(everyPage, totalCount);
int beginIndex = getBeginIndex(everyPage, currentPage);
boolean hasPrePage = getHasPrePage(currentPage);
boolean hasNextPage = getHasNextPage(totalPage, currentPage);
return new Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
// 以下方法辅助创建Page对象
public static int getEveryPage(int everyPage) { //获得每页显示记录数
return everyPage == 0 ? 10 : everyPage;
}
public static int getCurrentPage(int currentPage) { //获得当前页
return currentPage == 0 ? 1 : currentPage;
}
public static int getTotalPage(int everyPage,int totalCount) {//获得总页数
int totalPage = 0;
if(totalCount != 0 &&totalCount % everyPage == 0) {
totalPage = totalCount / everyPage;
} else {
totalPage = totalCount / everyPage + 1;
}
return totalPage;
}
public static int getBeginIndex(int everyPage,int currentPage) {//获得起始位置
return (currentPage - 1) * everyPage;
}
public static boolean getHasPrePage(int currentPage) {//获得是否有上一页
return currentPage == 1 ? false : true;
}
public static boolean getHasNextPage(int totalPage, int currentPage) { //获得是否有上一页
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
select 字段列表 from 表名 limit m,n;
m: 表示起始记录,并且从0开始
n: 查询记录的个数,每页记录数
分页信息
共多少页
有没有上一页
有没有下一页
当前页
注:分页信息类Page
注2:创建分页信息辅助类PageUtil
public static Page createPage(int everyPage,int totalCount,int currentPage)
everyPage: 程序员定
totalCount: 总记录数,查询数据库表记录 select count(*) from 表名
currentPage: 从默认第一页开始,下一页= 当前页+1 上一页 = 当前页-1
分页数据集合List
依据查询语句获得集合: select 字段列表 from 表名 limit m,n;
m: beginIndex
n: everyPage
具体实现:::
UserBiz:
//分页
public int getCont();
public List<User> findByPage(Page page);
123
UserBizImpl:
@Override
public int getCont() {
String sql = "select count(*) as count from user";
CountUtil count = (CountUtil) udao.get(sql, CountUtil.class);
return count.getCount();
}
@Override
public List<User> findByPage(Page page) {
String sql = "select * from user limit "+page.getBeginIndex()+", "+page.getEveryPage();
return udao.query(sql, User.class);
}
servlet::UserServlet
int everyPage = 5;//每页记录数
int totalCount = ubiz.getCont();//获取总记录数
//点击链接重新获取当前页
String scurrentPage = request.getParameter("currentPage");
int currentPage = 1; //当前页,默认1
if(scurrentPage == null){
currentPage = 1;//从第一页开始访问
}else{
currentPage = Integer.parseInt(scurrentPage);
}
//分页信息
Page page = PageUtil.createPage(everyPage, totalCount, currentPage);
//分页数据信息
List<User> list = ubiz.findByPage(page);
request.setAttribute("page", page);
request.setAttribute("list", list);
//转发到userlist.jsp
request.getRequestDispatcher("/back/manager/userlist.jsp").forward(request, response);
userlist.jsp中的分页表现
<table width="461" height="24" border="1" cellpadding="0" cellspacing="0">
<tr>
<td width="199">当前为第${page.currentPage}页,共${page.totalPage}页</td>
<td width="256">
<c:choose>
<c:when test="${page.hasPrePage}">
<a href="<%=path %>/user.do?method=list¤tPage=1">首页</a> |
<a href="<%=path %>/user.do?method=list¤tPage=${page.currentPage -1 }">上一页</a>
</c:when>
<c:otherwise>
首页 | 上一页
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page.hasNextPage}">
<a href="<%=path %>/user.do?method=list¤tPage=${page.currentPage + 1 }">下一页</a> |
<a href="<%=path %>/user.do?method=list¤tPage=${page.totalPage }">尾页</a>
</c:when>
<c:otherwise>
下一页 | 尾页
</c:otherwise>
</c:choose>
</td>
</tr>
</table>
附::::page类(分页信息实体类)
public class Page {
private int everyPage; //每页显示记录数
private int totalCount; //总记录数
private int totalPage; //总页数
private int currentPage; //当前页
private int beginIndex; //查询起始点
private boolean hasPrePage; //是否有上一页
private boolean hasNextPage; //是否有下一页
public Page(int everyPage, int totalCount, int totalPage,
int currentPage,int beginIndex, boolean hasPrePage,
boolean hasNextPage) { //自定义构造方法
this.everyPage = everyPage;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
}
///////get,set方法略
}123456789101112131415161718192021
附:pageutil 分页信息辅助类
public class PageUtil {
//创建Page对象
public static Page createPage(int everyPage,int totalCount,int currentPage) {//创建分页信息对象
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int totalPage = getTotalPage(everyPage, totalCount);
int beginIndex = getBeginIndex(everyPage, currentPage);
boolean hasPrePage = getHasPrePage(currentPage);
boolean hasNextPage = getHasNextPage(totalPage, currentPage);
return new Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
// 以下方法辅助创建Page对象
public static int getEveryPage(int everyPage) { //获得每页显示记录数
return everyPage == 0 ? 10 : everyPage;
}
public static int getCurrentPage(int currentPage) { //获得当前页
return currentPage == 0 ? 1 : currentPage;
}
public static int getTotalPage(int everyPage,int totalCount) {//获得总页数
int totalPage = 0;
if(totalCount != 0 &&totalCount % everyPage == 0) {
totalPage = totalCount / everyPage;
} else {
totalPage = totalCount / everyPage + 1;
}
return totalPage;
}
public static int getBeginIndex(int everyPage,int currentPage) {//获得起始位置
return (currentPage - 1) * everyPage;
}
public static boolean getHasPrePage(int currentPage) {//获得是否有上一页
return currentPage == 1 ? false : true;
}
public static boolean getHasNextPage(int totalPage, int currentPage) { //获得是否有上一页
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询