jsp+servlet分页查询

Posted poetryandyou

tags:

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

分页查询

  1. 减少服务器内存开销

  2. 提高用户体验

效果图

技术图片

思绪图

技术图片
技术图片

技术图片

分页显示Bean文件代码

package cn.ytmj.findlist.domain;

import java.util.List;

/**
 * @author rui
 * @create 2019-08-17 23:34
 * 分页对象
 * 使用泛型为多种页面提供服务
 */
public class PageBean<T> 
    private int totalCount;  //总记录数
    private int  totalPage; // 总页数
    private List<T> list;   //每页的数据list集合
    private int currentPage; //当前页码
    private int rows; //每页显示的条数
    public PageBean()

    public int getTotalCount() 
        return totalCount;
    

    public void setTotalCount(int totalCount) 
        this.totalCount = totalCount;
    

    public int getTotalPage() 
        return totalPage;
    

    public void setTotalPage(int totalPage) 
        this.totalPage = totalPage;
    

    public List<T> getList() 
        return list;
    

    public void setList(List<T> list) 
        this.list = list;
    

    public int getCurrentPage() 
        return currentPage;
    

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

    public int getRows() 
        return rows;
    

    public void setRows(int rows) 
        this.rows = rows;
    

    @Override
    public String toString() 
        return "PageBean" +
                "totalCount=" + totalCount +
                ", totalPage=" + totalPage +
                ", list=" + list +
                ", currentPage=" + currentPage +
                ", rows=" + rows +
                '';
    

FindUserByPageServlet代码

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        //获取数据
        String currentPage = request.getParameter("currentPage");
        String rows = request.getParameter("rows");
        if(null==currentPage||"".equals(currentPage))
            currentPage="1";
        
        if(null==rows||"".equals(rows))
            rows="5";
        
        //调用service
        UserService userService = new UserServiceImpl();
        PageBean<User> pageBean = userService.findUserByPage(Integer.parseInt(currentPage), Integer.parseInt(rows));
        request.setAttribute("pageBean", pageBean);
        //转发
        request.getRequestDispatcher("/list.jsp").forward(request, response);
    

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
        this.doPost(request, response);
    

service

PageBean<User> findUserByPage(int currentPage, int rows);

serviceimpl

   public class UserServiceImpl implements UserService 
    UserDao userDao = new UserDaoImpl();
   @Override
    public PageBean<User> findUserByPage(int currentPage, int rows) 
        PageBean<User> pageBean = new PageBean<>();
        if (currentPage <= 0) 
            currentPage = 1;
        
        int totalCount = userDao.findTotalCount();
        //计算总页数
        int totalPage = totalCount % ows == 0 ? totalCount / rows : totalCount / rows + 1;
        pageBean.setTotalPage(totalPage);
        if (currentPage > totalPage) 
            currentPage = totalPage;
        
        List<User> list = userDao.findUserByPage(currentPage, rows);

        pageBean.setTotalCount(totalCount);
        pageBean.setCurrentPage(currentPage);
        pageBean.setRows(rows);
        pageBean.setList(list);


        return pageBean;
    

dao

    //查询当前页面的所有数据
    List<User> findUserByPage(int currentPage, int rows);
    //总条数
    int findTotalCount();

daoimpl

  • 通过JDBCUtils获取DataSource

    package cn.ytmj.findlist.util;
    
    import com.alibaba.druid.pool.DruidDataSourceFactory;
    
    import javax.sql.DataSource;
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.Properties;
    
    /**
     * JDBC工具类 使用Durid连接池
     */
    public class JDBCUtils 
    
        private static DataSource ds ;
    
        static 
    
            try 
                //1.加载配置文件
                Properties pro = new Properties();
                //使用ClassLoader加载配置文件,获取字节输入流
                InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
                pro.load(is);
    
                //2.初始化连接池对象
                ds = DruidDataSourceFactory.createDataSource(pro);
    
             catch (IOException e) 
                e.printStackTrace();
             catch (Exception e) 
                e.printStackTrace();
            
        
    
        /**
         * 获取连接池对象
         */
        public static DataSource getDataSource()
            return ds;
        
    
    
        /**
         * 获取连接Connection对象
         */
        public static Connection getConnection() throws SQLException 
            return  ds.getConnection();
        
    
    
 public class UserDaoImpl implements UserDao 
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<User> findUserByPage(int currentPage, int rows) 
        String sql = "select * from user limit ? , ? ";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class), (currentPage - 1) * rows, rows);
        return list;
    

    @Override
    public int findTotalCount() 
        String sql = "select count(*) from user";
        int count = jdbcTemplate.queryForObject(sql,Integer.class);
        return count;
    
 

jsp页面分页显示相关代码

Bootstrap分页按钮模板(轻微修改),以备后用

            <div style="float: left">
                <nav>
                    <ul class="pagination">
                        <li>
                            <a href="#" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                        <li class="active"><a href="#">1 <span class="sr-only"></span></a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                        <li><a href="#">4</a></li>
                        <li><a href="#">5</a></li>
                        <li>
                            <a href="#" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                        <span style="font-size: 25px ;margin-left: 5px">共16条数据,共4页</span>
                    </ul>
                </nav>
            </div>

修改后jsp代码

 <div style="float: left">
                <nav>
                    <ul class="pagination">
                        <%-- 判断是否是第一页--%>
                        <c:if test="$pageBean.currentPage==1">
                        <li class="disabled">
                            </c:if>
                            <c:if test="$pageBean.currentPage!=1">
                        <li>
                            </c:if>
                            <a href="$pageContext.request.contextPath/findUserByPageServlet?currentPage=$pageBean.currentPage-1&rows=5"
                               aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                        <c:forEach var="i" varStatus="s" step="1" begin="1" end="$pageBean.totalPage">
                            <c:if test="$pageBean.currentPage == i">
                                <li class="active">

                                    <a href="$pageContext.request.contextPath/findUserByPageServlet?currentPage=$i&rows=5"

                                       name="li">$i</a></li>
                            </c:if>
                            <c:if test="$pageBean.currentPage != i">
                                <li>
                                    <a href="$pageContext.request.contextPath/findUserByPageServlet?currentPage=$i&rows=5"
                                       name="li">$i</a></li>
                            </c:if>
                        </c:forEach>
                        <%-- 判断是否是最后页--%>
                        <c:if test="$pageBean.currentPage >= pageBean.totalPage">
                        <li class="disabled">
                            </c:if>
                            <c:if test="$pageBean.currentPage!=pageBean.totalPage">
                        <li>
                            </c:if>
                            <a href="$pageContext.request.contextPath/findUserByPageServlet?currentPage=$pageBean.currentPage+1&rows=5"
                               aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                        <span style="font-size: 25px ;margin-left: 5px">共$pageBean.totalCount条数据,共$pageBean.totalPage页</span>
                    </ul>
                </nav>
            </div>
        </div>

以上是关于jsp+servlet分页查询的主要内容,如果未能解决你的问题,请参考以下文章

jsp+servlet实现模糊查询和分页效果

Servlet 分页保存查询条件

java的jsp如何分页显示查询结果呢?

分页,条件查询

jsp+sqlserver 2005模糊查询分页时候点击那个首页或下一页的时候老是查出所有

Oracle+Jsp分页