jsp 中实现分页的类模板

Page 类的代码如下:

package utils;

import java.util.ArrayList;
import java.util.List;
//分页类:对任何实体类的分页信息都能封装到这个bean中。
public class Page {//T代表任意类型。
	/***
	 * 当前页码,从客户端得到或手工设定
	 */
    private Integer currentPage;
	/***
	 * 总页码,从客户端得到或计算得到
	 */
    private Integer totalPage;
	/***
	 * 总记录条数,从数据库查询得到
	 */
    private Integer count;
	/***
	 * 一页中显示的详细信息的集合,从数据库查询得到
	 */
    private List pageList = new ArrayList();
	/***
	 * 每页的数据条数,从客户端得到或计算得到
	 */
    private Integer pageSize;
	public Page() {
		super();
	}
	public Page(Integer currentPage, Integer count,
			List pageList, Integer pageSize) {
		super();
		this.currentPage = currentPage;
		this.count = count;
		this.pageList = pageList;
		this.pageSize = pageSize;
	}
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public Integer getTotalPage() {
		if(count%pageSize==0){
			totalPage = count/pageSize;
		}else{
			totalPage = count/pageSize+1;
		}
		return totalPage;
	}
	public Integer getCount() {
		return count;
	}
	public void setCount(Integer count) {
		this.count = count;
	}
	public List getPageList() {
		return pageList;
	}
	public void setPageList(List pageList) {
		this.pageList = pageList;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

}

运用如下:

假如要分页显示用户信息,用户类如下:

package com.yibin.cn.dao.entity;

import java.util.Date;

public class User implements java.io.Serializable{
	private Integer id;
	private String name;
	private String password;
	private String sex;
	private Date birthday;
	private String address;
	private String picturePath;

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

	public User(Integer id, String name, String password, String sex,
			Date birthday, String address, String picturePath) {
		super();
		this.id = id;
		this.name = name;
		this.password = password;
		this.sex = sex;
		this.birthday = birthday;
		this.address = address;
		this.picturePath = picturePath;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPicturePath() {
		return picturePath;
	}

	public void setPicturePath(String picturePath) {
		this.picturePath = picturePath;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password
				+ ", sex=" + sex + ", birthday=" + birthday + ", address="
				+ address + ", picturePath=" + picturePath + "]";
	}

}


	/***
	 * 查询某一页的详细数据
	 * @param currentPage 要查询的页码
	 * @param pageSize  每页记录数
	 * @return 成功则返回一页详细数据的集合,失败则返回空(null)
	 */
	public List getOnePageInfo(int currentPage,int pageSize){
		if(currentPage<1) {
			System.out.println("注意页码是从1开始的!");
			return null;
		}
		if(pageSize<1) {
			System.out.println("注意每页显示条数是从1开始的!");
			return null;
		}
		List users = new ArrayList();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet res = null;
//sqlPageInfo:SELECT id,`sex`,`name`,`birthday`,`password`,`address`,`picture_path` FROM `user` LIMIT (currentPage-1)*pageSize,pageSize.		
//sqlPageInfo:SELECT id,`sex`,`name`,`birthday`,`password`,`address`,`picture_path` FROM `user` LIMIT 1,2.		
        String sqlPageInfo = "SELECT id,`sex`,`name`,`birthday`,`password`,`address`,`picture_path` FROM `user` LIMIT ?,?";
		try {
			conn = jdbcUtil.getConnection();
			ps = conn.prepareStatement(sqlPageInfo);
			ps.setObject(1, (currentPage-1)*pageSize);
			ps.setObject(2, pageSize);
			res = ps.executeQuery();
			while(res.next()){
				User user = new User();
				user.setId(res.getInt("id"));
				user.setName(res.getString("name"));
				user.setPassword(res.getString("password"));
				user.setSex(res.getString("sex"));
				user.setBirthday(res.getDate("birthday"));
				user.setAddress(res.getString("address"));
				user.setPicturePath(res.getString("picture_path"));
			    users.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			jdbcUtil.closeAll(conn, ps, res);
		}
        return users;
	}
	
	/***
	 * 方法功能说明:把一页的全部信息都封装起来
	 * @param currentPage 要查询的页码
	 * @param pageSize  每页记录数
	 * @return
	 */
	    public Page getPage(int currentPage,int pageSize){
	    	int count = this.getCount();
	    	List pageList = this.getOnePageInfo(currentPage, pageSize);
	    	return new Page(currentPage, count, pageList, pageSize);
	    }








你可能感兴趣的:(Java(Jsp))