javaweb分頁原理詳解
更新時間:2017年04月08日 14:06:39 作者:第九種格調(diào)的人生
這篇文章主要為大家詳細(xì)介紹了javaweb分頁的原理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了javaweb分頁原理的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
public class Page { private int currentPage; private int totalPage; private int count; private int PageSize; private List<Product> list; private String category; }
servlet:
package com.learning.web.servlet; import java.io.IOException; import java.util.List; import javax.enterprise.inject.New; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.learning.domain.Page; import com.learning.domain.Product; import com.learning.service.ProductService; @WebServlet("/showProductByPage") public class ShowProductByPage extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int currentPage=1; int pageSize=4; //第一次取為空 String currentPageString=request.getParameter("currentPage"); if (currentPageString!=null) { currentPage=Integer.parseInt(currentPageString); } String category = request.getParameter("category"); if ("".equals(category)) { category=null; } ProductService productService=new ProductService(); Page page=productService.showProductByPage(currentPage,pageSize,category); request.setAttribute("page", page); request.getRequestDispatcher("/product_list.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
service:
public Page showProductByPage(int currentPage, int pageSize, String category) { try { Page page=new Page(); int count=productDao.count(category); page.setCount(count); page.setList(productDao.findProductsByPage(currentPage,pageSize,category)); int totalPage=(int) Math.ceil(1.0*count/pageSize); page.setPageSize(pageSize); page.setCurrentPage(currentPage); page.setTotalPage(totalPage); page.setCategory(category); return page; } catch (SQLException e) { e.printStackTrace(); } return null; }
Dao:
public int count(String category) throws SQLException { QueryRunner queryRunner=new QueryRunner(C3P0Util.getDataSource()); String sql=" select count(*) from products "; if (category!=null) { sql+=" where category='"+category+"'"; } long l= (Long)queryRunner.query(sql, new ScalarHandler(1)); return (int) l; } public List<Product> findProductsByPage(int currentPage, int pageSize, String category) throws SQLException { QueryRunner queryRunner=new QueryRunner(C3P0Util.getDataSource()); String sql=" select * from products "; if (category!=null) { sql+=" where category='"+category+"'"; } sql+=" limit ?,?"; return queryRunner.query(sql, new BeanListHandler<Product>(Product.class),(currentPage-1)*pageSize,pageSize); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中分頁插件PageHelper的使用詳解
分頁查詢是為了高效展示大量數(shù)據(jù),通過分頁將數(shù)據(jù)劃分為多個部分逐頁展示,原生方法需手動計(jì)算數(shù)據(jù)起始行,而使用PageHelper插件則簡化這一過程,本文給大家介紹SpringBoot中分頁插件PageHelper的使用,感興趣的朋友一起看看吧2024-09-09springboot中使用@Transactional注解事物不生效的坑
這篇文章主要介紹了springboot中使用@Transactional注解事物不生效的原因,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01JavaBean valication驗(yàn)證實(shí)現(xiàn)方法示例
這篇文章主要介紹了JavaBean valication驗(yàn)證實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了JavaBean valication驗(yàn)證相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03Java設(shè)計(jì)模式之創(chuàng)建者模式詳解
這篇文章主要介紹了Java設(shè)計(jì)模式之創(chuàng)建者模式詳解,創(chuàng)建者模式,顧名思義,就是提供友好的創(chuàng)建對象的方式?,對象都是?new?出來的,但是在一些情況下,這種方式不是很友好,首先,它不夠直觀,需要的朋友可以參考下2023-08-08