实现JSP分页方法一:手写

1.创建Pager类
package com.yan.shop.model;

import java.util.List;





public class Pager {
	
 /**
 * 当前页码
 */
private int pageIndex;
 
 /**
 * 每页数目
 */
private int pageSize;

 /**
 * 总共条数
 */
private int totalRecord;

 /**
 * 总页数
 */
private int totalPage;
 /**
 * 具体内容
 */
private List datas;
public int getPageIndex() {
	return pageIndex;
}
public void setPageIndex(int pageIndex) {
	this.pageIndex = pageIndex;
}
public int getPageSize() {
	return pageSize;
}
public void setPageSize(int pageSize) {
	this.pageSize = pageSize;
}
public int getTotalRecord() {
	return totalRecord;
}
public void setTotalRecord(int totalRecord) {
	this.totalRecord = totalRecord;
}
public int getTotalPage() {
	return totalPage;
}
public void setTotalPage(int totalPage) {
	this.totalPage = totalPage;
}
public List getDatas() {
	return datas;
}
public void setDatas(List datas) {
	this.datas = datas;
} 
}
2.通过jdbc按照条件填充列表
public Pager list(int pageIndex,int pageSize) {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Pager page=new Pager();
		List users=new ArrayList();
		User u=null;
		try{
			if(pageIndex<=0)
			{pageIndex=1;}
			int start=(pageIndex-1)*pageSize;
		con=DBUtil.getConnection();
		String sql="select * from t_user limit ?,?";
		String sqlCount="select count(*) from t_user";
		ps=con.prepareStatement(sql);
		ps.setInt(1, start);
		ps.setInt(2, pageSize );
		rs=ps.executeQuery();
		while(rs.next()){
			u=new User();
			u.setId(rs.getInt("id"));
			u.setNickname(rs.getString("nickname"));
			u.setPassword(rs.getString("password"));
			u.setUsername(rs.getString("username"));
			u.setStatus(rs.getInt("status"));
			u.setType(rs.getInt("type"));
			users.add(u);
		}
		ps=con.prepareStatement(sqlCount);
		rs=ps.executeQuery();
		int totalRecord=0;
		while(rs.next()){
			totalRecord=rs.getInt(1);
		}
		int totalPage=(totalRecord-1)/pageSize+1;
		page.setPageIndex(pageIndex);
		page.setPageSize(pageSize);
		page.setTotalPage(totalPage);
		page.setTotalRecord(totalRecord);
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtil.close(rs);
			DBUtil.close(ps);
			DBUtil.close(con);
		}
		
		page.setDatas(users);
		return page;
	}
 
  

3.应用,这是list.jsp页面,list是userdao的一个方法,故先创建userdao,通过list()实例化一个page

<%@page import="com.yan.shop.model.Pager"%>
<%@page import="com.yan.shop.model.User"%>
<%@page import="java.util.List"%>
<%@page import="com.yan.shop.dao.IUserDao"%>
<%@page import="com.yan.shop.dao.DAOFactory"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here
<%
IUserDao userDao=DAOFactory.getUserDao();
int pageIndex=1;
try{
pageIndex=Integer.parseInt(request.getParameter("pageIndex"));
}catch(NumberFormatException e){}
int pageSize=1;
Pager pa=userDao.list(pageIndex,pageSize);
List users=pa.getDatas();
User loginUser=(User)session.getAttribute("loginUser");
%>






<%for(User u:users){
	%>
	
<%} 	
} 
%>
用户标识 用户名 用户密码 用户昵称 用户类型 用户状态 操作
<%=u.getId() %> <%=u.getUsername() %> <%=u.getPassword() %> <%=u.getNickname() %> <%if(u.getType()==0){ %>普通用户 <%if(loginUser.getType()==1) {%> 设置管理员<% } }else{ %>管理员<%if(loginUser.getType()==1) {%>取消管理员<%} } %> <%if(u.getStatus()==0){ %>启用 <%if(loginUser.getType()==1) {%>停用 <%} }else{ %>停用<%if(loginUser.getType()==1) {%>启用<%} } %> <% if(loginUser.getType()==1){%> 删除 更新
一个有<%=pa.getTotalPage() %>页,当前为第<%=pa.getPageIndex() %>页
<%if(pageIndex==1){%>[上一页]<% }else{%>[上一页]<% }%> [首页] <% int totalPage=pa.getTotalPage(); for(int i=1;i<=totalPage;i++) { if(i==pageIndex){ out.println(i); }else{ %> [<%=i %>] <%} }%> [尾页] <%if(pageIndex==totalPage){%>[下一页]<% }else{%>[下一页]<% }%>


 
 

你可能感兴趣的:(jsp)