Java Web分页技术
Java Web分页技术(2009-02-27 08:49:01)转载标签: it 分类: 技术分类
关于在java web上实现分页技术,方式实际上有很多,也各有个的特点,此处我只写些我的认识。java web分页无外乎两种,一种是直接取出来,放到一个集合里,通过传begin 和 end 参数控制分页,还有一种就是把分页工作交给数据库,让数据库读取需要的begin~end之间的数据。
我们这里,先看从数据库中读取的情况
操作数据库就需要tsql语句,mssqlserver2005新推出了一个row_number()很好用,还有就是mysql的limit也非常好使。
mssqlserver2005的如下:
select * from (select row_number() over (order by ename) as rn, f.* from emp f) b
where b.rn between 6 and 10;
mysql的:
select * from emp limit 5,5
mysql的应注意,使用limit时,表中必须用主键,还有limit后的两个参数分别代表(标识位,长度),标识位从0开始
现在开始一步步完成,首先完成model模块,建立pagebean
import java.util.*;
public class PageBean {
private Collection objs;//从数据库中读的集合
private int totalCount;//总的条数
private int pageNo;//当前的页数
private int pageCount;//每页的条数
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public int getTotalPage(){
if(totalCount % pageCount == 0){
return totalCount/pageCount;
} else {
return totalCount/pageCount + 1;
}
}
//多写一个判断下一页的方法
public boolean isNext(){
return pageNo < getTotalPage();
}
//上一页的方法
public boolean isPrevious(){
return pageNo > 1;
}
public Collection getObjs() {
return objs;
}
public void setObjs(Collection objs) {
this.objs = objs;
}
public int getPageNo() {
return pageNo;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public PageBean(Collection objs, int totalCount, int pageNo, int pageCount) {
this.objs = objs;
this.totalCount = totalCount;
this.pageNo = pageNo;
this.pageCount = pageCount;
}
}
之后开始实现biz模块
我们写一个具体分页的逻辑
import java.util.*;
import java.sql.*;
public class EmpBiz
{
public EmpBiz() {
}
//具体实现分页的方法,传递两个参数,一个第几页,一个每页的数量
public PageBean listEmps(int pageNo, int pageCount){
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList emps = new ArrayList();
//次数我使用mssqlserver的方式,所以计算前后两个范围,用a,b表示
int a = pageNo* pageCount;
int b = (pageNo-1)* pageCount;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Master;","sa","sa");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from (select row_number() over (order by ename) as rn, f.* from emp f) b
where b.rn between "+a+" and "+b);
while(rs.next()){
Employee e = new Employee();
e.setEmpno(rs.getInt("empno"));
e.setEname(rs.getString("ename"));
e.setSal(rs.getDouble("sal"));
emps.add(e);
}
rs = stmt.executeQuery("select count(*) from emp");
int totalCount=0;
if(rs.next()){
totalCount = rs.getInt(1);
}
PageBean pageBean = new PageBean(emps,totalCount,pageNo,pageCount);
return pageBean;
}
catch (Exception ex) {
System.out.println("发生错误,错误是:" + ex.getMessage());
return null;
} finally {
if(stmt != null) {
try { stmt.close(); } catch (SQLException ex1) {}
}
if(con != null) {
try { con.close(); } catch (SQLException ex1) {}
}
}
}
}
实现完EmpBiz之后,开始写servlet类
import java.io.*;
import java.util.*;
public class EmpServlet
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
EmpBiz biz = new EmpBiz();
//给页数和每页数量一个初始值
int pageNo = 1;
int pageCount = 5;
String pageNoStr = request.getParameter("pageNo");
String pageCountStr = request.getParameter("pageCount");
if(pageNoStr != null){
pageNo = Integer.parseInt(pageNoStr);
}
if(pageCountStr != null){
pageCount = Integer.parseInt(pageCountStr);
}
PageBean pageBean = biz.listEmps(pageNo,pageCount);
request.setAttribute("pageBean",pageBean);
request.getRequestDispatcher("listemp.jsp").forward(request,response);
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletExcep
tion, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}
业务逻辑完了之后,只要实现jsp页面即可
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="https://www.360docs.net/doc/2d5586140.html,/jsp/jstl/core" prefix="c" %>
listemp