1、关于Java分页实现的流程图
2、在分页的实现中,关键的几个类,以及代码的实现,QueryInfo,QueryResult,PageBean
QueryInfo的代码实现
public classQueryInfo {private int currentpage = 1; //用户当前看的页
private int pagesize = 5; //记住用户想看的页面大小
private int startindex; //记住用户看的页的数据在数据库的起始位置
public intgetCurrentpage() {returncurrentpage;
}public void setCurrentpage(intcurrentpage) {this.currentpage =currentpage;
}public intgetPagesize() {returnpagesize;
}public void setPagesize(intpagesize) {this.pagesize =pagesize;
}public intgetStartindex() {this.startindex = (this.currentpage-1)*this.pagesize;returnstartindex;
}
}
QueryResult的代码实现
public classQueryResult {private List list; //记住用户看的页的数据
private int totalrecord; //记往总记录数
publicList getList() {returnlist;
}public voidsetList(List list) {this.list =list;
}public intgetTotalrecord() {returntotalrecord;
}public void setTotalrecord(inttotalrecord) {this.totalrecord =totalrecord;
}
}
PageBean的代码实现
public classPageBean {privateList list;private inttotalrecord;private intpagesize;private inttotalpage;private intcurrentpage;private intpreviouspage;private intnextpage;private int[] pagebar;publicList getList() {returnlist;
}public voidsetList(List list) {this.list =list;
}public intgetTotalrecord() {returntotalrecord;
}public void setTotalrecord(inttotalrecord) {this.totalrecord =totalrecord;
}public intgetPagesize() {returnpagesize;
}public void setPagesize(intpagesize) {this.pagesize =pagesize;
}public intgetTotalpage() {//100 5 20//101 5 21//99 5 20
if(this.totalrecord%this.pagesize==0){this.totalpage = this.totalrecord/this.pagesize;
}else{this.totalpage = this.totalrecord/this.pagesize+1;
}returntotalpage;
}public intgetCurrentpage() {returncurrentpage;
}public void setCurrentpage(intcurrentpage) {this.currentpage =currentpage;
}public intgetPreviouspage() {if(this.currentpage-1<1){this.previouspage = 1;
}else{this.previouspage = this.currentpage-1;
}returnpreviouspage;
}public intgetNextpage() {if(this.currentpage+1>=this.totalpage){this.nextpage = this.totalpage;
}else{this.nextpage = this.currentpage +1;
}returnnextpage;
}public int[] getPagebar() {intstartpage;intendpage;int pagebar[] = null;if(this.totalpage<=10){
pagebar= new int[this.totalpage];
startpage= 1;
endpage= this.totalpage;
}else{
pagebar= new int[10];
startpage= this.currentpage - 4;
endpage= this.currentpage + 5;//总页数=30 3 -1//总页数=30 29 34 21 30
if(startpage<1){
startpage= 1;
endpage= 10;
}if(endpage>this.totalpage){
endpage= this.totalpage;
startpage= this.totalpage - 9;
}
}int index = 0;for(int i=startpage;i<=endpage;i++){
pagebar[index++] =i;
}this.pagebar =pagebar;return this.pagebar;/*int pagebar[] = new int[this.totalpage];
for(int i=1;i<=this.totalpage;i++){
pagebar[i-1] = i;
}
this.pagebar = pagebar;
return pagebar;*/}
}
Student实体类的代码
packagecn.itcast.domain;public classStudent {privateString id;privateString name;publicString getId() {returnid;
}public voidsetId(String id) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}
}
3、关于dao层StudentDao,serive层StudentSerive,controller层StudentServlet层的实现
关于dao层StudentDao
importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.util.ArrayList;importjava.util.List;importcn.itcast.domain.QueryResult;importcn.itcast.domain.Student;importcn.itcast.utils.JdbcUtils;public classStudentDao {public QueryResult pageQuery(int startindex,intpagesize){
Connection conn= null;
PreparedStatement st= null;
ResultSet rs= null;
QueryResult qr= newQueryResult();try{
conn=JdbcUtils.getConnection();
String sql= "select * from student limit ?,?";
st=conn.prepareStatement(sql);
st.setInt(1, startindex);
st.setInt(2, pagesize);
rs=st.executeQuery();
List list= newArrayList();while(rs.next()){
Student s= newStudent();
s.setId(rs.getString("id"));
s.setName(rs.getString("name"));
list.add(s);
}
qr.setList(list);
sql= "select count(*) from student";
rs=conn.prepareStatement(sql).executeQuery();if(rs.next()){
qr.setTotalrecord(rs.getInt(1));
}returnqr;
}catch(Exception e) {throw newRuntimeException(e);
}finally{
JdbcUtils.release(conn, st, rs);
}
}
}
serive层StudentSerive
importcn.itcast.dao.StudentDao;importcn.itcast.domain.PageBean;importcn.itcast.domain.QueryInfo;importcn.itcast.domain.QueryResult;public classStudentService {publicPageBean pageQuery(QueryInfo info){
StudentDao dao= newStudentDao();
QueryResult qr=dao.pageQuery(info.getStartindex(), info.getPagesize());
PageBean bean= newPageBean();
bean.setCurrentpage(info.getCurrentpage());
bean.setList(qr.getList());
bean.setPagesize(info.getPagesize());
bean.setTotalrecord(qr.getTotalrecord());returnbean;
}
}
controller层StudentServlet层的实现
importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcn.itcast.domain.PageBean;importcn.itcast.domain.QueryInfo;importcn.itcast.service.BusinessService;importcn.itcast.utils.WebUtils;public class ListStudentServlet extendsHttpServlet {public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
QueryInfo info= WebUtils.request2Bean(request, QueryInfo.class);
BusinessService service= newBusinessService();
PageBean bean=service.pageQuery(info);
request.setAttribute("pagebean", bean);
request.getRequestDispatcher("/liststudent.jsp").forward(request, response);
}public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
doGet(request, response);
}
}
4、用到的工具类JdbcUtils和WebUtils
工具类JdbcUtils
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.Properties;public classJdbcUtils {private static Properties config = newProperties();static{try{
config.load(JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"));
Class.forName(config.getProperty("driver"));
}catch(Exception e) {throw newExceptionInInitializerError(e);
}
}public static Connection getConnection() throwsSQLException{return DriverManager.getConnection(config.getProperty("url"), config.getProperty("username"), config.getProperty("password"));
}public static voidrelease(Connection conn,Statement st,ResultSet rs){if(rs!=null){try{
rs.close();//throw new
}catch(Exception e) {
e.printStackTrace();
}
rs= null;
}if(st!=null){try{
st.close();
}catch(Exception e) {
e.printStackTrace();
}
st= null;
}if(conn!=null){try{
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
db.properties的配置
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/student
username=root
password=root
工具类WebUtils
importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.Map;importjava.util.UUID;importjavax.servlet.http.HttpServletRequest;importorg.apache.commons.beanutils.BeanUtils;importorg.apache.commons.beanutils.ConvertUtils;importorg.apache.commons.beanutils.Converter;public classWebUtils {public static T request2Bean(HttpServletRequest request,ClassbeanClass){try{
T bean=beanClass.newInstance();//得到request里面所有数据
Map map =request.getParameterMap();//map{name=aa,password=bb,birthday=1990-09-09} bean(name=aa,password=dd,birthday=Date)
ConvertUtils.register(newConverter(){publicObject convert(Class type, Object value) {if(value==null){return null;
}
String str=(String) value;if(str.trim().equals("")){return null;
}
SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd");try{returndf.parse(str);
}catch(ParseException e) {throw newRuntimeException(e);
}
}
}, Date.class);
BeanUtils.populate(bean, map);returnbean;
}catch(Exception e) {throw newRuntimeException(e);
}
}public staticString generateID(){returnUUID.randomUUID().toString();
}
}
关于页面的显示,主要利用EL表达式和JSTL来获取数据
显示学生分页数据${s.id } ${s.name }
function gotopage(currentpage){
var pagesize = document.getElementById("pagesize").value;
window.location.href = '${pageContext.request.contextPath}/servlet/ListStudentServlet?currentpage=' + currentpage + '&pagesize=' + pagesize;
}共[${pagebean.totalrecord }]条记录,
每页条,
共[${pagebean.totalpage }]页,
当前[${pagebean.currentpage }]页
上一页
${pagenum }
${pagenum }
下一页