jsp分页

来源:互联网 发布:js定义json数组并赋值 编辑:程序博客网 时间:2024/06/05 19:12

后台第一步创建分页类pagebean

代码如下:

package unity;


public class PageBean {


private int countPage = 8; // 多少条记录进行分页


private int countRecords; // 记录总数


public static int totalPage; // 总页数


private int currentPage; // 当前页


public int getCountPage() {
return countPage;
}


public void setCountPage(int countPage) {
this.countPage = countPage;
}


public int getCountRecords() {
return countRecords;
}


public void setCountRecords(int countRecords) {
if (countRecords % countPage == 0) {
this.totalPage = countRecords / countPage;
} else {
this.totalPage = countRecords / countPage + 1;
}
this.countRecords = countRecords;
}


// 总页数
public int getTotalPage() {
return totalPage;
}


public int getCurrentPage() {
return currentPage;
}


public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}


}

第二步编写SQL语句查询

第三部前台将当前页数传入后台,通过页数和每页的条数的设定确定结果


第四部将结果放到list集合并传到前台,将pagebean对象也传入前台,




前端jstl

第一步在项目lab下导入jstl的jar包

第二步 引入头文件 格式如下:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

第三部将后台传入的pagebean对象和list集合,通过jstl表达式判断接收

具体代码见下:

<tr>
<td>
<span>
<c:if test="${pagebean.currentPage!=1}"><a href = "<%=basePath %>queryuser?currentpage=1">首页</a></c:if>
</span>
</td>
<td>
<span>
<c:if test="${(pagebean.currentPage)<(pagebean.totalPage)}"><a href = "<%=basePath %>queryuser?currentpage=${((pagebean.currentPage)+1)}">下一页</a></c:if>
</span>
</td>
<td>
<span>
<c:if test="${(pagebean.currentPage)>1}"><a href = "<%=basePath %>queryuser?currentpage=${((pagebean.currentPage)-1)}">上一页</a></c:if>
</span>
</td>
<td>
<span>
<c:if test="${(pagebean.currentPage)<(pagebean.totalPage)}"><a href = "<%=basePath %>queryuser?currentpage=${(pagebean.totalPage)}">末页</a></c:if>
</span>
</td>
<td>
<span>
当前页${(pagebean.currentPage)}/${(pagebean.totalPage)}
</span>
</td>
</tr>

遍历如下:

<c:forEach var='user' items="${list}" varStatus="">
<tr>
<td class="first w4 c">${user.username}</td>
<td class="w1 c">${user.real_name}</td>
<td class="w2 c">${user.sex}</td>
<td>${user.email}</td>
<td class="w4 c">${user.phone}</td>
<td class="w1 c"><a href="<%=basePath %>updateuser?userid=${user.user_id}">修改</a> <a class="manageDel" href="<%=basePath %>deleteuser?userid=${user.user_id}">删除</a></td>
</tr>


0 0
原创粉丝点击