当前位置: 首页 > 工具软件 > jspf > 使用案例 >

JSPf分页技术

有权
2023-12-01

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

 类似资料: