一、分页工具类
package blank.util;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
public class Pagination<T> {
// 每页的数量
private long pageSize;
// 当前页
private long nowPage;
// 总页数
private long pages;
// 每页开始的记录数
private long startSize;
// 当前页的数据
private List<T> rows;
// 总记录数
private long countSize;
public Pagination() {
super();
}
public Pagination(long pageSize, long nowPage, String tableName) {
super();
// 每页显示的数量
this.pageSize = pageSize;
// 总记录数
this.countSize =getCount(tableName);
// 计算出总页数
this.pages = this.countSize % this.pageSize == 0 ? this.countSize
/ this.pageSize : this.countSize / this.pageSize + 1;
// 判断当前页如果 小于等于0
if (nowPage <= 0) {
this.nowPage = 1; // 第一页
} else if (nowPage > this.pages) { // 如果当前页大于总页数 ,那么当前页应该等于总页数
this.nowPage = this.pages;
} else {
this.nowPage = nowPage; // 当前页就等于当前页
}
// 计算出开始的记录数
this.startSize = (this.nowPage - 1) * pageSize;
}
public long getCount(String tableName) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(DBConn.getDataSource());
// 定义查询对象
long count = jdbcTemplate.queryForInt("select count(*) from "+tableName);
//返回总记录数
return count;
}
public List<T> getRows() {
return rows;
}
public void setRows(List<T> rows) {
this.rows = rows;
}
public long getPageSize() {
return pageSize;
}
public long getNowPage() {
return nowPage;
}
public long getPages() {
return pages;
}
public long getStartSize() {
return startSize;
}
public long getCountSize() {
return countSize;
}
@Override
public String toString() {
return "Pagination [pageSize=" + pageSize + ", nowPage=" + nowPage
+ ", pages=" + pages + ", startSize=" + startSize + ", rows="
+ rows + ", countSize=" + countSize + "]";
}
}
二、应用
(1)实体定义
public class Pet {
private Integer id;
private String name;
private String owner;
private String species;
private String sex;
private Date birth;
private Date death;
}
(2)数据库操作接口
public interface PetDao {
boolean insert(Pet entity);
boolean delete(Pet entity);
boolean delete(Integer id);
boolean update(Pet entity);
List<Pet> getObjects();
Pet getObjectsById(Integer id);
List<Pet> getObjects(long startSize, long pageSize);
}
(3)页面展示层(首页面list.jsp)
<table border="2">
<caption>宠物登记信息</caption>
<tr>
<th>select</th>
<th>id</th>
<th>name</th>
<th>owner</th>
<th>sex</th>
<th>species</th>
<th>birth</th>
<th>death</th>
<th>options</th>
</tr>
<c:forEach var="p" items="${pagination.rows}">
<tr>
<td><input type="checkbox" name="state" /></td>
<td>${p.id}</td>
<td>${p.name}</td>
<td>${p.owner}</td>
<td>${p.sex}</td>
<td>${p.species}</td>
<td>${p.birth}</td>
<td>${p.death}</td>
<td><a href="./pet.do?oper=update&id=${p.id}">update</a>
<a href="./pet.do?oper=delete&id=${p.id}">delete</a>
<a href="./pet.do?oper=details&id=${p.id}">details</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="4"><a href="info.jsp">Insert</a></td>
<td colspan="5"><a href="./pet.do?oper=deleteSelect">DeleteSelected</a></td>
</tr>
<tr>
<td colspan="9">
<a href="./pet.do?oper=list&nowPage=1">首页</a>
<a href="./pet.do?oper=list&nowPage=${pagination.nowPage+1}">下一页</a>
<a href="./pet.do?oper=list&nowPage=${pagination.nowPage-1 }">上一页</a>
<a href="./pet.do?oper=list&nowPage=${pagination.pages}">末页</a>
</td>
</tr>
</table>
(4)servlet控制层
private PetDao petDao = new PetDaoImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String value = request.getParameter("oper");
if ("list".equals(value)) {
listPets(request, response);
} else if ("insert".equals(value)) {
insertPet(request, response);
} else if ("delete".equals(value)) {
deletePet(request, response);
} else if ("update".equals(value)) {
updateById(request, response);
}else if ("updateOk".equals(value)) {
updatePet(request, response);
} else if ("details".equals(value)) {
detailsPet(request, response);
}
}
private void listPets(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String snowPage=request.getParameter("nowPage");
long nowPage=1;
if(snowPage!=null){
nowPage=Long.valueOf(snowPage);
}
Pagination<Pet> pagination=new Pagination<Pet>(3,nowPage, "pet");
List<Pet> entities=petDao.getObjects(pagination.getStartSize(),3);
pagination.setRows(entities);
request.setAttribute("pagination", pagination);
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
private void updateById(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("id");
Pet pet=null;
pet=petDao.getObjectsById(Integer.valueOf(id));
if(pet!=null){
request.setAttribute("pet", pet);
request.getRequestDispatcher("/update.jsp").forward(request, response);
}
}
private void updatePet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("id");
String nameValue = request.getParameter("name");
String ownerValue = request.getParameter("owner");
String sexValue = request.getParameter("sex");
String speciesValue = request.getParameter("species");
String birthValue = request.getParameter("birth");
String deathValue = request.getParameter("death");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Pet pet = new Pet(Integer.valueOf(id), nameValue, ownerValue, speciesValue, sexValue,
sdf.parse(birthValue), sdf.parse(deathValue));
boolean flag=petDao.update(pet);
if(flag){
request.getRequestDispatcher("/pet.do?oper=list")
.forward(request, response);
}else{
request.getRequestDispatcher("/pet.do?oper=update")
.forward(request, response);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
private void detailsPet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("id");
Pet pet=null;
pet=petDao.getObjectsById(Integer.valueOf(id));
if(pet!=null){
request.setAttribute("pet", pet);
request.getRequestDispatcher("/details.jsp")
.forward(request, response);
}else{
request.getRequestDispatcher("/pet.do?oper=list")
.forward(request, response);
}
}
private void deletePet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
boolean flag = petDao.delete(Integer.valueOf(id));
if (flag) {
request.getRequestDispatcher("/pet.do?oper=list")
.forward(request, response);
} else {
request.getRequestDispatcher("/pet.do?oper=list")
.forward(request, response);
}
}
private void insertPet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String nameValue = request.getParameter("name");
String ownerValue = request.getParameter("owner");
String sexValue = request.getParameter("sex");
String speciesValue = request.getParameter("species");
String birthValue = request.getParameter("birth");
String deathValue = request.getParameter("death");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Pet pet = null;
try {
pet = new Pet(null, nameValue, ownerValue, speciesValue, sexValue,
sdf.parse(birthValue), sdf.parse(deathValue));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
boolean flag = petDao.insert(pet);
if (flag) {
List<Pet> pets = petDao.getObjects();
request.setAttribute("pets", pets);
request.getRequestDispatcher("/pet.do?oper=list")
.forward(request, response);
} else {
request.setAttribute("pet", "信息填写错误");
request.getRequestDispatcher("/info.jsp")
.forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}