欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javaweb分页原理详解

程序员文章站 2024-03-02 21:20:16
本文实例为大家分享了javaweb分页原理的具体实现代码,供大家参考,具体内容如下 public class page { private int cu...

本文实例为大家分享了javaweb分页原理的具体实现代码,供大家参考,具体内容如下

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);
 }


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。