package org.thj.bookstore.util;
@SuppressWarnings("unused")
public class Pager {
private int currentPage;
private int pageSize = 3;
private int totalSize;
private int totalPage;
private boolean hasFirst;
private boolean hasPrevious;
private boolean hasNext;
private boolean hasLast;
public Pager(int currentPage,int totalSize){
this.currentPage = currentPage;
this.totalSize = totalSize;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public boolean isHasFirst() {
if(currentPage == 1)
return false;
return true;
}
public void setHasFirst(boolean hasFirst) {
this.hasFirst = hasFirst;
}
public boolean isHasLast() {
if(currentPage == getTotalPage())
return false;
return true;
}
public void setHasLast(boolean hasLast) {
this.hasLast = hasLast;
}
public boolean isHasNext() {
if(isHasLast())
return true;
return false;
}
public void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
public boolean isHasPrevious() {
if(isHasFirst())
return true;
return false;
}
public void setHasPrevious(boolean hasPrevious) {
this.hasPrevious = hasPrevious;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
totalPage = totalSize / pageSize;
if(totalSize % pageSize != 0)
totalPage++;
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
}
============
实现类:
public class BookAction extends ActionSupport {
protected ICatalogService catalogService;
protected IBookService bookService;
protected Integer catalogid;
private Integer currentPage=1;
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getCatalogid() {
return catalogid;
}
public void setCatalogid(Integer catalogid) {
this.catalogid = catalogid;
}
public ICatalogService getCatalogService() {
return catalogService;
}
public void setCatalogService(ICatalogService catalogService) {
this.catalogService = catalogService;
}
public IBookService getBookService() {
return bookService;
}
public void setBookService(IBookService bookService) {
this.bookService = bookService;
}
public String browseBook() throws Exception {
int totalSize = bookService.getTotalByCatalogid(catalogid);
Pager pager = new Pager(currentPage,totalSize);
List books = bookService.getBookByCatalogid(catalogid, currentPage, pager.getPageSize());
Map request = (Map)ActionContext.getContext().get("request");
Map session = ActionContext.getContext().getSession();
session.put("catalogid",catalogid);
request.put("books",books);
request.put("pager",pager);
return SUCCESS;
}
}
==================
<%@ page contentType="text/html; charset=gb2312"%>
<%@ taglib prefix="ww" uri="/webwork"%>
<jsp:include page="../head.jsp"></jsp:include>
<html>
<head>
<title>图书浏览</title>
</head>
<body bgcolor="#ccff99">
<br />
<br />
<br />
以下是您所选的图书:<br/>
<ww:set name="pager" value="#request.pager"/>
<table width="500">
<ww:iterator value="#request['books']" id="book">
<tr height="140" align="center">
<td>书名:<ww:property value="#book.bookname"/></td>
<td>价格:<ww:property value="#book.price"/></td>
<td><img src="<ww:property value="#book.picture"/>"></td>
<td>
<form action="addToCart.action" method="post">
数量:<input type="text" name="quantity" value="0" size="4"/>
<input type="hidden" value="<ww:property value="#book.bookid"/>" name="bookid"/>
<input type="submit" value="加入购物车"/>
</form>
</td>
</tr>
</ww:iterator>
</table>
<ww:if test="#pager.hasFirst">
<a href="browseBook.action?currentPage=1">首页</a>
</ww:if>
<ww:if test="#pager.hasPrevious">
<a href="browseBook.action?currentPage=<ww:property value="#pager.currentPage-1"/>">上一页</a>
</ww:if>
<ww:if test="#pager.hasNext">
<a href="browseBook.action?currentPage=<ww:property value="#pager.currentPage+1"/>">下一页</a>
</ww:if>
<ww:if test="#pager.hasLast">
<a href="browseBook.action?currentPage=<ww:property value="#pager.totalPage"/>">尾页</a>
</ww:if>
<br/>
当前第<ww:property value="#pager.currentPage"/>页,总共<ww:property value="#pager.totalPage"/>页
</body>
</html>