一、PageBean内容如下
package com.kingming.entity;
import java.util.List;
public class PageBean {
private int currPage;
private int prePage;
private int nextPage;
private int limitSize;
private int countSize;
private int startIndex;
private List data;
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getPrePage() {
return this.currPage>1?this.currPage-1:this.currPage;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
return this.currPage<this.countSize?this.currPage+1:this.currPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getLimitSize() {
return limitSize;
}
public void setLimitSize(int limitSize) {
this.limitSize = limitSize;
}
public int getCountSize() {
return countSize;
}
public void setCountSize(int countSize) {
this.countSize = countSize%this.limitSize==0?countSize/this.limitSize:countSize/this.limitSize+1;
}
public int getStartIndex() {
return (this.currPage-1)*this.limitSize;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
public PageBean(String currPage, String limitSize) {
if(currPage==null){
this.currPage = 1;
}else{
this.currPage = Integer.parseInt(currPage);
}
if(limitSize==null){
this.limitSize = 2;
}else{
this.limitSize = Integer.parseInt(limitSize);
}
}
}
二、创建Dao层
package com.kingming.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.naming.NamingException;
import com.kingming.entity.MsgEntity;
import com.kingming.entity.PageBean;
public class MsgDao extends DBUtils{
public List<MsgEntity> getMyMsgList(){
String sql = "select * from msg";
ResultSet rs = null;
List<MsgEntity> list = new ArrayList<MsgEntity>();
try {
rs = super.queryResult(sql);
while(rs.next()){
list.add(new MsgEntity(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getString(6),rs.getString(7)));
}
return list;
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
return null;
}
public PageBean getMyMsgListWithPage(PageBean pageBean){
String sql = "select * from msg limit ?,?";
ResultSet rs = null;
List<MsgEntity> list = new ArrayList<MsgEntity>();
try {
rs = super.queryResult(sql,pageBean.getStartIndex(),pageBean.getLimitSize());
while(rs.next()){
list.add(new MsgEntity(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getInt(5),rs.getString(6),rs.getString(7)));
}
pageBean.setCountSize(this.getMyMsgList().size());
pageBean.setData(list);
return pageBean;
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}
return null;
}
}
三、创建Biz
package com.kingming.biz;
import java.util.List;
import com.kingming.dao.MsgDao;
import com.kingming.entity.MsgEntity;
import com.kingming.entity.PageBean;
public class MsgBiz {
MsgDao dao = new MsgDao();
public List<MsgEntity> getMyMsgList(){
return dao.getMyMsgList();
}
public PageBean getMyMsgListWithPage(PageBean pageBean){
return dao.getMyMsgListWithPage(pageBean);
}
}
四、Control层
package com.kingming.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.kingming.biz.MsgBiz;
import com.kingming.entity.PageBean;
public class MsgAction extends HttpServlet {
MsgBiz biz = new MsgBiz();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String page = request.getParameter("p");
String size = request.getParameter("s");
PageBean pageBean = new PageBean(page,size);
pageBean = biz.getMyMsgListWithPage(pageBean);
request.setAttribute("PB",pageBean);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
五、JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<ul>
<c:forEach items="${PB.data }" var="temp">
<li>${temp.msgid }----${temp.title }</li>
</c:forEach>
</ul>
<div>
<a href="/PageDemo/msg">首页</a>
<a href="/PageDemo/msg?p=${PB.prePage}&s=${PB.limitSize}">上一页</a>
<a href="/PageDemo/msg?p=${PB.nextPage}&s=${PB.limitSize}">下一页</a>
<a href="/PageDemo/msg?p=${PB.countSize}&s=${PB.limitSize}">尾页</a>
</div>
</body>
</html>
个人笔记记录一下
|