JSPf分页技术

1.创建pagaBean

package com.mipo.Been;

import java.util.List;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;

public class PageBeen {
private int pageSize;//每页显示多少条
private int rowCount;//显示一共有多少条数据
private int pageTotal;//总页数
private int currentPage;//当前页码
private List<?> data;//当前页显示多少条数据



public PageBeen() {
this.pageSize=6;
}

public PageBeen(int pageSize, int rowCount, int pageTotal, int currentPage, List<?> data) {
super();
this.pageSize = pageSize;
this.rowCount = rowCount;
this.pageTotal = pageTotal;
this.currentPage = currentPage;
this.data = data;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getRowCount() {
return rowCount;
}

public void setRowCount(int rowCount) {
this.rowCount = rowCount;
this.pageTotal=(int)Math.ceil(this.rowCount*1.0/this.pageSize);
}

public int getPageTotal() {
return pageTotal;
}

public void setPageTotal(int pageTotal) {
this.pageTotal = pageTotal;
}

public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
if(currentPage<1){
this.currentPage=1;
}else if(currentPage>pageTotal){
this.currentPage=pageTotal;
}else{
this.currentPage=currentPage;
}
}

public List<?> getData() {
return data;
}
public void setData(List<?> data) {
this.data = data;
}

}
2.controller

String currentPage=req.getParameter("currentPage");
String pageSize=req.getParameter("pageSize");
if(null==currentPage||0==currentPage.trim().length()){
currentPage="1";
}
if(null==pageSize||0==pageSize.trim().length()){
pageSize="6";
}
String catalog=req.getParameter("coid");
BookDao dao=new BookDaoImp();
PageBeen pageBeen=new PageBeen();
pageBeen.setRowCount(dao.rowNum());
pageBeen.setPageSize(Integer.parseInt(pageSize));
pageBeen.setCurrentPage(Integer.parseInt(currentPage));
List<Map<String, Object>> list=dao.showAllbooks(pageBeen.getCurrentPage(),pageBeen.getPageSize());
pageBeen.setData(list);
// 这里没有给pageTotal赋值,因为pageTotal是算出来的值
req.setAttribute("pageBeen",pageBeen);
req.getRequestDispatcher("category.jsp").forward(req, resp);

3.dao// 根据条件查询
public List<Map<String, Object>> search(Phoner phoner,Integer uid,int currentPage, int pageSize) {
List<Map<String, Object>> list=null;
DataSource ds=DataSourceHelper.getDS();
String name=phoner.getNam();
String sex1=phoner.getSex();
String birth=phoner.getBirth();
String phone=phoner.getTel();

Integer group=phoner.getTid();

String sql="SELECT c.cid,c.nam,c.sex,c.tel,c.birth,(SELECT cy.tnam FROM ctype AS cy WHERE cy.tid=c.tid)AS tnam FROM contact AS c WHERE c.uid=?";
if(null!=name&&0!=name.trim().length()){
sql+=" and nam like"+" '%"+name+"%'";
}
if(null!=sex1){
sql+=" and sex='"+sex1+"'";
}
if(null!=phone&&0!=phone.trim().length()){
sql+=" and tel like"+" '%"+phone+"%'";
}
if(null!=birth&&0!=birth.trim().length()){
sql+=" and birth like"+" '%"+birth+"%'";

}
if(0!=group){
sql+=" and tid="+group+"";
}
sql+=" limit ?,?";

try {
list=new QueryRunner(ds).query(sql, new MapListHandler(),uid,(currentPage-1)*pageSize,pageSize);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return list;
}

4.分页查询

public List<Map<String, Object>> showAllbooks(int currentPage,int pageSize) {
DataSource ds=DataSourceHelper.getDS();
String sql="SELECT tp.poid,(SELECT tc.cname FROM tcatalog AS tc WHERE tc.coid=tp.coid)AS coid,tp.pname,tp.publisher,tp.author,tp.price,tp.imagename,tp.introment,tp.total,tp.pstatus,tp.hot FROM tproduct AS tp ";
sql+=" limit ?,?";
try {
return new QueryRunner(ds).query(sql,new MapListHandler(),(currentPage-1)*pageSize,pageSize);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值