天天看点

Java web 实现分页功能jsp分页功能如何实现

jsp分页功能如何实现

后端

整体逻辑是:前端发送来的请求(包含需求页面数和每页数量),由servlet捕获后调用相关的service层,service选择性做出更改继而调用dao层和数据库进行交互。

1.新建pageBean泛型类,包括但不限于以下成员变量

private int totalNumber;
private int currentPage;
private int rows;
private int totalPage;
private List<T> lists;  
           
2.service层(已目录为例),写出业务逻辑
public interface CategoryService {
    int addCategory(Category category);
    ArrayList<Category> allCategory();
    boolean updateCategory(String oldCname, String newCname);
    boolean deleteCategory(String cname);
    PageBean<Category> pageInfo(int page, int rows);
}  
           
3.dao层,实现数据增删改查
public interface CategoryDao {
    boolean queryCategoryByName(Category category);
    int addCategory(Category category);
    ArrayList<Category> allCategory();
    boolean deleteCategory(String cname);
    boolean updateCategory(String oldCname,String newCname);
    PageBean<Category> pageInfo(int page, int rows);
}  
           
4.sql查询语句(mysql)
public ArrayList<Category> partCategory(int page, int rows) {
    // 分页查询语句
    String sql = "select * from category limit " + page*rows + "," + rows;
    List<Map<String, Object>> maps = template.queryForList(sql);
    ArrayList<Category> categories = new ArrayList<>();
    for (Map<String, Object> map : maps) {
        Category category = new Category();
        try {
            // 封装对象
            BeanUtils.populate(category, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        categories.add(category);
    }
    return categories;
}
           
5.将前端需要的数据结果,和总页数(总条数除以每页条数rows)、总条数(使用count(*)查询)封装进pageBean,设置session属性,将页面转发回前端

前端

可显示所有页码,也可仅显示前一页或后一页,此处使用后者。若要使用前者可使用如下进行遍历打印所有页码,然后设置function(page)响应每个页码的onclick。

<c:forEach begin="1" end="${pageBeans.totalPage}" step="1">

1.展示总页码,当前页码,总条数(使用EL表达式从pageBeans中获取数据)

共有 p a g e B e a n s . t o t a l N u m b e r &lt; / s t r o n g &gt; 条 记 录 , 当 前 第 &lt; s t r o n g &gt; {pageBeans.totalNumber}&lt;/strong&gt; 条记录,当前第&lt;strong&gt; pageBeans.totalNumber</strong>条记录,当前第<strong>{pageBeans.currentPage} 页,共 ${pageBeans.totalPage} 页

2.设置首页、尾页(使用时要放在标签内)

a href="KaTeX parse error: Expected 'EOF', got '&' at position 72: …findAllCategory&̲page=1&rows=5">…{pageContext.request.contextPath }/CategoryServlet?op=findAllCategory&rows=5&page=${pageBeans.totalPage }">尾页
3.跳转页(设置一个text框输入数据,然后使用jump函数判断)
<script type="text/javascript">
	function jump() {
		var numa = document.getElementById("numa").value;
		if (!/^[1-9][0-9]*$/.test(numa)) {
			alter("页码不对");
			return;
		}
		if (numa > ${pageBeans.totalPage}) {
			alert("超出范围了");
			return;
		}
		window.location.href = "${pageContext.request.contextPath }/CategoryServlet?op=findAllCategory&rows=5&page="
				+ numa;
	}
</script>  
           
4.前一页、后一页(通过判断当页是否为第一页或最后一页,来选择性执行,页面实际从0开始,所有比较值为0)
<c:if test="${pageBeans.currentPage > 0}">
	<a href="${pageContext.request.contextPath }/CategoryServlet?op=findAllCategory&&rows=5&page=${pageBeans.currentPage}" target="_blank" rel="external nofollow" >上一页</a>
</c:if>
<c:if test="${pageBeans.currentPage == 0}">
	<a class="disabled" href="#" target="_blank" rel="external nofollow" >上一页</a>
</c:if>
           

容易出错的地方

1.servlet接受的请求应带有page和rows,若没有这两个输入则会输出空的Arraylist.

2.jsp中使用EL表达式获取pageBeans的数据时变量名错误

3.分页各部分跳转路径中要包含page和rows,且op方法要相同。