在JSP页面中实现分页显示和翻页功能,需要来回传递哪几个参数?

在JSP页面中实现分页显示和翻页功能,需要来回传递哪几个参数?,第1张

页面分页通常有两种展现形式:

查询出全部结果数据,以集合等形式保存在内存中租猜,每次在内存中读取一页的数据显示。该方法首次加载数据量较大,耗时会很久,而且可能展现出的数据可能包含被修改或删除过的过期或垃圾数据,存储数据也会消耗大量的内存,但首次加载后,分页展现会非常迅速,效果较好。

每次切页时从数据库中检索当前页所需展现数据,每次查询数较少,总体开销也就减少了,再进行SQL优化,也能达到较高的效率,而且实弊轮型时检索不易出现数据错误的问题。

使用分页功能,最关键的参数如下:

请求参数:

1)当前需要展示的页码,变量,默认从第一页开始,可能是页面上的上下页,通过当前页码±1来计算出来,也可能是页面有页码页表用户通过点击相应数字或是输入框用户手输入的页码;

2)每页显示的数量,通常是变量,可以从页码提供相应的下拉框供用户选择。若是定义为常量,那就不需要每次传递了;

3)总数量,根据筛选条件决定,若是筛选条件固定,则桐喊只需将此定义为常量,不必作为参数传输,否则则需要根据筛选条件每次查询数据库获取计数。

返回参数:

返回需要展示的列表及以上请求参数,通常列表通过Ajax计数实现,那也就不需要返回请求参数了。所展示的列表通常会使用集合类型进行封装或是数据读取成json格式由前台进行解析。

分页雀肆梁依据:

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&currentPage=1">首页</a>|

<a href="<%=path %>/user.do?method=list&currentPage=${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&currentPage=${page.currentPage + 1 }">下一页</a>|

<a href="<%=path %>/user.do?method=list&currentPage=${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

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12234592.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存