分页

本文介绍了一个简单的图书管理系统中实现分页展示的方法。通过定义Pager类来处理页面跳转逻辑,并结合BookAction类从数据库获取图书数据,实现了图书按页显示的功能。此外,还展示了如何在JSP页面中使用迭代标签和条件标签来展示图书信息和分页导航。
摘要由CSDN通过智能技术生成

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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值