java--分页技术

Posted xanlv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java--分页技术相关的知识,希望对你有一定的参考价值。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>

  <body>
    <c:redirect url="/page"></c:redirect>
  </body>
</html>

show.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
  <head>
    <script type="text/javascript">
        function sub(obj)
            window.location.href="<c:url value='/page?pageNo="+obj.value+"'/>";
        
    </script>
  </head>

  <body>
    <h3>学生信息</h3>
    <c:forEach items="$result.students" var="s">
        id=$s.id,,,name=$s.name<br/>
    </c:forEach>

    pageCount=$result.pageCount<br/>
    &nbsp;   &nbsp;   &nbsp;   &nbsp;
    <select onchange="sub(this)">
        <c:forEach begin="1" end="$result.pageCount" var="idx">
            <%--
                <option selected='<c:if test="$idx==result.currentPage">selected</c:if>' value="$idx">第$idx页</option>
             --%>
            <option <c:if test="$idx==result.currentPage">selected</c:if> value="$idx">第$idx页</option>
        </c:forEach>
    </select>
    <br/><br/>
    <c:if test="$result.currentPage>1">
        <a href='<c:url value="/page?pageNo=$result.currentPage-1"></c:url>'>上一页</a>&nbsp;
    </c:if>
    <c:forEach begin="1" end="$result.pageCount" var="idx">
        <c:if test="$result.currentPage==idx">
            $idx
        </c:if>
        <c:if test="$result.currentPage!=idx">
            <a href='<c:url value="/page?pageNo=$idx"></c:url>'>$idx</a>&nbsp;
        </c:if>
    </c:forEach>
    <c:if test="$result.currentPage<result.pageCount">
        <a href='<c:url value="/page?pageNo=$result.currentPage+1"></c:url>'>下一页</a>&nbsp;
    </c:if>
    <br/><br/>   &nbsp;   &nbsp;   

  </body>
</html>

C3p0Utils.java见http://blog.csdn.net/xanlv/article/details/53266493

值对象Stud.java

package cn.hncu.domain;

public class Stud 

    private String id;
    private String name;

    public String getId() 
        return id;
    
    public void setId(String id) 
        this.id = id;
    
    public String getName() 
        return name;
    
    public void setName(String name) 
        this.name = name;
    

    @Override
    public String toString() 
        return "stud =" + id + "," + name;
    


servlet层
PageServlet.java

package cn.hncu.servlet;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hncu.service.IPageService;
import cn.hncu.service.PageServiceImpl;


public class PageServlet extends HttpServlet 

    //依赖注入service
    IPageService service=new PageServiceImpl();
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
        doPost(request, response);
    

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 

        String pageNo=request.getParameter("pageNo");
        if(pageNo==null||pageNo.equals(""))
            pageNo="1";//默认查询第1页
        

        try 
            //把页码转成int
            int iPageNo=Integer.parseInt(pageNo);
            Map<String, Object> result=service.query(iPageNo);
//          System.out.println("--"+result.get("pageCount"));
//          System.out.println("--"+result.get("students"));
            //再补一个结果3: 当前页码
            result.put("currentPage", iPageNo);
            //结果数据一定要放在容器中
            request.setAttribute("result", result);
            //转发到结果页面去显示
            request.getRequestDispatcher("/jsps/show.jsp").forward(request, response);
         catch (NumberFormatException e) 
            response.getWriter().println("页码格式错误!");
         catch (Exception e) 
            response.getWriter().println("数据库查询错误!");
        
    


service层

package cn.hncu.service;

import java.util.Map;

public interface IPageService 

    public Map<String, Object> query(Integer iPageNo) throws Exception ;

package cn.hncu.service;

import java.util.Map;

import cn.hncu.dao.PageDAO;
import cn.hncu.dao.PageDaoJdbc;

public class PageServiceImpl implements IPageService 

    //依赖注入dao
    PageDAO dao=new PageDaoJdbc();
    @Override
    public Map<String, Object> query(Integer iPageNo) throws Exception 
        return dao.query(iPageNo);
    


dao层

package cn.hncu.dao;

import java.util.Map;

public interface PageDAO 

    public Map<String, Object> query(Integer iPageNo) throws Exception ;

package cn.hncu.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.hncu.pubs.C3p0Utils;

public class PageDaoJdbc implements PageDAO 

    @Override
    public Map<String, Object> query(Integer iPageNo) throws Exception 
        int pageSize=20;//每页显示10行,本例这里写死了
        Map<String, Object> result=new HashMap<String,Object>();
        //查询总行数
        String sql_1="select count(1) from student";
        QueryRunner run=new QueryRunner(C3p0Utils.getDataSource());
        Object obj=run.query(sql_1, new ScalarHandler());//使用ScalarHandler封装返回结果是1行1列的数据
//      System.out.println("pageCount:"+obj.toString());
        int rows=Integer.parseInt(obj.toString());
        //计算出总页数
//      int pageCount=rows/iPageNo+rows%iPageNo==0?0:1;
        int pageCount=rows/pageSize+(rows%pageSize==0?0:1);
        result.put("pageCount", pageCount);//结果1
        System.out.println("row:"+rows);
        System.out.println("pageCount:"+pageCount);
        //查询当前页要显示的内容(数据行)
        int startN = (iPageNo-1)*pageSize;//从哪一行开始显示
        String sql_2="select * from student limit "+startN+","+pageSize;
//      List<Map<String, String>> students=run.query(sql_2, new MapListHandler());//error
        List<Map<String, Object>> students=run.query(sql_2, new MapListHandler());
//      System.out.println(students);
        result.put("students", students);//结果2
//      System.out.println("ll"+result.get("students"));
        return result;
    


访问项目的初始页面

显示第10页

最后一页

以上是关于java--分页技术的主要内容,如果未能解决你的问题,请参考以下文章

java分页中怎么实现当本页面最后一条记录被删除,自动向上一个页面跳转

SpringBoot 整合PageHelper分页 ,超过最大页数时一直返回最后一页的数据

关于Java的分页算法,急!

java分页怎么从最后一页开始显示

java 分页模型的模板

已经查询出来的数据如何在java页面实现分页?