利用ajax技术实现通用分页,在jsp页面呈现

 
  
package com.softeem.dto;//dto数据传输对象


import java.util.ArrayList;
import java.util.List;


public class PageBean {
private int currentPage; // 当前页码
private int pageSize; // 每页记录条数


private int totalNum; // 总数据条数
private int totalPage; // 总页码数
private int first = 1; // 首页
private int pre; // 上一页
private int next; // 下一页
private int last; // 尾页


private List list = new ArrayList<>(); // 数据集


public int getCurrentPage() {
return currentPage;
}


public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}


public int getPageSize() {
return pageSize;
}


public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}


public int getTotalNum() {
return totalNum;
}


public void setTotalNum(int totalNum) {
this.totalNum = totalNum;
}


public int getTotalPage() {
return totalPage;
}


public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}


public int getFirst() {
return first;
}


public void setFirst(int first) {
this.first = first;
}


public int getPre() {
return pre;
}


public void setPre(int pre) {
this.pre = pre;
}


public int getNext() {
return next;
}


public void setNext(int next) {
this.next = next;
}


public int getLast() {
return last;
}


public void setLast(int last) {
this.last = last;
}


public List getList() {
return list;
}


public void setList(List list) {
this.list = list;
}


}
package com.softeem.dao;//数据访问对象


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.softeem.ac.dbutils.DBUtils;
import com.softeem.ac.dbutils.DBUtils.CallBack;
import com.softeem.dto.Daily;
import com.softeem.dto.PageBean;




public class DailyDAO implements BaseDAO {


@Override
public List findByPage(PageBean bean) {
String sql = "select _id,content,submitTime,lastTime from daily limit ?,?";
int start = (bean.getCurrentPage() - 1) * bean.getPageSize();
return DBUtils.queryList(sql, new CallBack() {
@Override
public List getDatas(ResultSet rs) {
List list = new ArrayList();
try {
Daily daily = null;
while (rs.next()) {
daily = new Daily(rs.getInt("_id"), rs
.getString("content"), rs
.getTimestamp("submitTime"), rs
.getTimestamp("lastTime"));


list.add(daily);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}, start, bean.getPageSize());
}


@Override
public int getCount() {//获取总页数
String sql = "select count(*) from daily";
return DBUtils.queryOne(sql, new CallBack() {
@Override
public Integer getData(ResultSet rs) {
try {
if (rs.next()) {
return rs.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
});
}


}


package com.softeem.service;//业务逻辑层


import java.util.List;


import com.softeem.dao.BaseDAO;
import com.softeem.dto.PageBean;


public class PaginationService {
private BaseDAO dao;


public PaginationService(BaseDAO dao) {
this.dao = dao;
}


public PageBean paging(PageBean bean) {
int totalNum = dao.getCount();//获取总页数
if (totalNum % bean.getPageSize() == 0) {//记录当前页数
bean.setTotalPage(totalNum / bean.getPageSize());
} else {
bean.setTotalPage(totalNum / bean.getPageSize() + 1);
}


if (bean.getCurrentPage() < 1) {//当前页不能小于1
bean.setCurrentPage(1);
}
if (bean.getCurrentPage() > totalNum) {//当前页不能大于页面总数
bean.setCurrentPage(totalNum);
}
bean.setTotalNum(totalNum);
bean.setTotalPage(bean.getTotalPage());
bean.setPre(bean.getCurrentPage() - 1);
bean.setNext(bean.getCurrentPage() + 1);
bean.setLast(bean.getTotalPage());
List list = dao.findByPage(bean);
bean.setList(list);//把数据装进集合
return bean;
}
}
package com.softeem.servlet;


import java.io.IOException;
import java.io.PrintWriter;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson.JSON;
import com.softeem.dao.DailyDAO;
import com.softeem.dto.Daily;
import com.softeem.dto.PageBean;
import com.softeem.service.PaginationService;




/**
 * Servlet implementation class DailyServlet
 */
@WebServlet("/DailyServlet")
public class DailyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
       
  
    public DailyServlet() {
        super();
    }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String cp = request.getParameter("cp");
int currentPage = cp == null?1:Integer.parseInt(cp);//判断获取的页数不为空,防止空指针异常

PageBean bean = new PageBean();
bean.setCurrentPage(currentPage);
bean.setPageSize(5);//每5条数据分一页

PaginationService service = new PaginationService(new DailyDAO());
bean = service.paging(bean);

String json = JSON.toJSONString(bean);//把java对象转成json对象
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.print(json);
out.flush();
}


}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>




Insert title here















编号日志内容提交时间修改时间






 
 

你可能感兴趣的:(Java,MySQL,jsp,servlet,html,javascript)