servlet+jsp實現分頁
1.PageBean
/**
* 這是一個用於封裝了分頁的資料
* 包含:
* 該頁的學生集合資料
* 總的記錄數
* 總的頁數
* 當前頁
* 每頁顯示的記錄數
* @author acer
*
*/
public class PageBean<T> {
private int currentPage;//當前頁
private int totalPage;//總頁數
private int pageSize;//每頁的記錄數
private int totalSize;//總的記錄數
private List<T> list;//當前頁的學生集合
public int getCurrentPage() {
return currentPage;
}
注意給出setter getter方法
2.Dao介面
List<Student> findStudentByPage(int currentPage) throws SQLException;
/**
* 查詢總的學生記錄數
*/
int findCount() throws SQLException;
3.Dao實現
@Override
public List<Student> findStudentByPage(int currentPage) throws SQLException {
String sql = "select * from stus limit ? offset ?";
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSouece());
return runner.query(sql, new BeanListHandler<Student>(Student.class),PAGE_SIZE,(currentPage-1)*PAGE_SIZE);
}
@Override
public int findCount() throws SQLException {
String sql = "select count(*) from stus";
QueryRunner runner = new QueryRunner(JDBCUtil02.getDataSouece());
Long result = (Long)runner.query(sql, new ScalarHandler());
return result.intValue();
}
4.service介面
/**
* 查詢當頁資料
* @param currentPage
* @return
* @throws SQLException
*/
PageBean findStudentByPage(int currentPage) throws SQLException;
5.service實現
public PageBean findStudentByPage(int currentPage) throws SQLException {
// 封裝分頁的該頁資料
PageBean<Student> pageBean = new PageBean<Student>();
int pageSize = StudentDao.PAGE_SIZE;
pageBean.setCurrentPage(currentPage);//設定當前頁
pageBean.setPageSize(pageSize);//設定每頁顯示多少記錄
StudentDao dao = new StudentDaoImpl();
List<Student> list = new StudentDaoImpl().findStudentByPage(currentPage);
pageBean.setList(list);//設定這一頁的學生資料
//總的記錄數,總的頁數
int count = dao.findCount();
pageBean.setTotalSize(count);//設定總的記錄數
pageBean.setTotalPage(count % pageSize==0 ? count / pageSize : (count / pageSize) + 1);//總頁數
return pageBean;
}
6.servlet控制層
/**
* 分頁顯示學生列表的servlet
*/
public class StuListPageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//1.獲取需要顯示的頁碼數
int currentPage = Integer.parseInt(request.getParameter("currentPage"));
//2.根據指定的頁面,去獲取該頁面的資料
StudentService service = new StudentServiceImpl();
PageBean pageBean = service.findStudentByPage(currentPage);
request.setAttribute("pageBean", pageBean);
//3.跳轉頁面
request.getRequestDispatcher("list_page.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
7.前臺顯示:
<c:forEach items="${pageBean.list }" var="stu">
<tr align="center">
<td>${stu.sid }</td>
<td>${stu.sname }</td>
<td>${stu.gender }</td>
<td>${stu.phone }</td>
<td>${stu.birthday }</td>
<td>${stu.hobby }</td>
<td>${stu.info }</td>
<td><a href="EditStuServlet?sid=${stu.sid }">更新</a> <a
href="#" onclick="doDelete(${stu.sid })">刪除</a></td>
</tr>
</c:forEach>
<tr>
<td colspan="8">第${pageBean.currentPage } /
${pageBean.totalPage } 每頁顯示${pageBean.pageSize }條
總記錄數${pageBean.totalSize }
<c:if test="${pageBean.currentPage !=1 }">
<a href="StuListPageServlet?currentPage=1">首頁</a> |
<a href="StuListPageServlet?currentPage=${pageBean.currentPage-1 }">上一頁</a>
</c:if> <c:forEach begin="1"
end="${pageBean.totalPage }" var="i">
<c:if test="${pageBean.currentPage == i }">
${i }
</c:if>
<c:if test="${pageBean.currentPage != i }">
<a href="StuListPageServlet?currentPage=${i }">${i }</a>
</c:if>
</c:forEach> <c:if
test="${pageBean.currentPage !=pageBean.totalPage }">
<a
href="StuListPageServlet?currentPage=${pageBean.currentPage+1 }">下一頁</a> |
<a href="StuListPageServlet?currentPage=${pageBean.totalPage }">尾頁</a>
</c:if>
</td>
</tr>