java分页

《tag.tid》

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd ">
 <tlib-version>1.0</tlib-version>
 <short-name>mytag</short-name> 
 <tag>
  <name>page</name>
  <tag-class>com.accp.tag.PageTag</tag-class>
  <body-content>empty</body-content>
  <attribute>
   <name>actionPath</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <name>pageCount</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>
  <attribute>
   <name>currentPage</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.lang.String</type>
  </attribute>  
 </tag>
</taglib>

《dao接口》
 public int count();
 public List find(int pageSize,int pageNumber);

《daoImpl接口实现类》

  //查询总条数
 public int count()
 {  
  Session session = super.getSession();

  String hql="select count(*) from GoodsInfo as goodsinfo";
  Query query=session.createQuery(hql);
  int count=Integer.parseInt(query.uniqueResult().toString());
  return count;
 }
 //分页查询
  public List find(int pageSize,int pageNumber)
  {
   Session session = super.getSession();
   String hql=" select from GoodsInfo as goodsinfo order by goodsinfo.goodsId";
   Query query=session.createQuery(hql); 
   int first=pageSize*(pageNumber-1);  
   query.setFirstResult(first);
   query.setMaxResults(pageSize);
  return query.list();
  }
《biz接口》

  public int count();
  public List find(int pageSize,int pageNumber);

《bizImpl接口实现类》

 private IGoodsInfoDAO goodsinfoDao = null;
 public void setGoodsinfoDao(IGoodsInfoDAO goodsinfoDao) {
  this.goodsinfoDao = goodsinfoDao;
 }
 public int count() {
    return  goodsinfoDao.count();
  
 }
 public List find(int pageSize, int pageNumber) {
  List list=goodsinfoDao.find(pageSize, pageNumber);
  return list;
 }

<page方法类类>

public class Page {
 private int pageNumber=1;
    private int pageCount;
    private int pageSize=5;
    public Page()
    {
     this.count();
    }
    public void count()
    {
     GoodsInfoDAOHibImpl goodsinfo=new GoodsInfoDAOHibImpl();
     int i=goodsinfo.count();
     this.pageCount=(i+pageSize-1)/pageSize;
    }
 public int getPageCount() {
  return pageCount;
 }
 public void setPageCount(int pageCount) {
  this.pageCount = pageCount;
 }
 public int getPageNumber() {
  return pageNumber;
 }
 public void setPageNumber(int pageNumber) {
  this.pageNumber = pageNumber;
 }
 public int getPageSize() {
  return pageSize;
 }
 public void setPageSize(int pageSize) {
  this.pageSize = pageSize;

 

<PageTag 类>

public class PageTag extends SimpleTagSupport {
     private String actionPath;
     private String currentPage;
     private String pageCount;
    
     public void doTag() throws JspException, IOException { 
   super.doTag(); 
    JspWriter out =this.getJspContext().getOut();
    out.print("<a href="+actionPath+"&&curPage="+1+">");
    out.print("第一页");
    out.print("</a>");
   
    if(Integer.parseInt(currentPage)>1){
    
     out.print("<a href="+actionPath+"&&curPage="+(Integer.parseInt(currentPage)-1)+">");
    }
    out.print("上一页");
    if(Integer.parseInt(currentPage)>1){
     out.print("</a");
    }
    if(pageCount.equals("0")){
     pageCount="1";
    }
   
    for(int i=1;i<Integer.parseInt(pageCount);i++){
    
     if(i==Integer.parseInt("pageCount")){
      out.print("<a href="+actionPath+"&&curPage="+i+"><font color='red'>i</font></a>");
      out.print("&nbsp");
     }else{
     
      out.print("<a href="+actionPath+"&&curPage="+i+">"+i+"</a>");
      out.print("&nbsp");
     }
       } 
    if(Integer.parseInt(currentPage)<Integer.parseInt(pageCount)){
     out.print("<a href="+actionPath+"&&curPage="+(Integer.parseInt(currentPage)+1)+">");
    }
    out.print("下一页");
    if(Integer.parseInt(currentPage)<Integer.parseInt(pageCount)){
     out.print("</a>");
    }
     out.print("<a href="+actionPath+"&&curPage="+Integer.parseInt(pageCount)+">");
     out.print("最后一页");
     out.print("</a>");
  }
 public String getActionPath() {
  return actionPath;
 }

 public void setActionPath(String actionPath) {
  this.actionPath = actionPath;
 }

 public String getCurrentPage() {
  return currentPage;
 }

 public void setCurrentPage(String currentPage) {
  this.currentPage = currentPage;
 }

 public String getPageCount() {
  return pageCount;
 }

 public void setPageCount(String pageCount) {
  this.pageCount = pageCount;
 }
}

 <action方法中>

    private IGoodsInfoBiz goodsinfoBiz =null;
   
 public void setGoodsinfoBiz(IGoodsInfoBiz goodsinfoBiz) {
  this.goodsinfoBiz = goodsinfoBiz;
 }
   //查询全部的商品分页显示
 public ActionForward doGoodsInfoList(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  GoodsinfoForm goodsinfoForm = (GoodsinfoForm) form;
  
  int page;
  try{ 
   page=Integer.parseInt(request.getParameter("curPage"));
  
  }catch(Exception ex){
   
   page=1;
  }
  Page mypage = new Page();
  mypage.setPageNumber(page);
  request.setAttribute("mypage",mypage );
  List goodsInfoList=goodsinfoBiz.find(mypage.getPageSize(), mypage.getPageNumber());
  request.setAttribute("goodsInfoList",goodsInfoList); 
  return mapping.findForward("goodsInfoList");
 }

《jsp页面》

<%@ taglib uri="WEB-INF/tag.tld" prefix="w"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

 </html:html>

  <head></head>

  <body>
    <table width="427" height="244" border="1">
  <tr bgcolor="#666666">
    <td height="30">编号</td>
    <td>名称</td>
    <td>价格</td>
    <td>数量</td>
    <td>描述</td>
  
  </tr>
 
  <logic:iterate id="goodsInfo" name="goodsInfoList">
  <tr>
   <td height="30"><bean:write name="goodsInfo" property="goodsId"/></td>
    <td><bean:write name="goodsInfo" property="goodsName"/></td>
    <td><bean:write name="goodsInfo" property="goodsPrice"/></td>
    <td><bean:write name="goodsInfo" property="goodsNumber"/></td>
    <td><bean:write name="goodsInfo" property="goodsDesc"/></td>
    </tr>
    </logic:iterate>
  <tr>
  <w:page actionPath="goodsinfo.do?operate=doGoodsInfoList" pageCount="${mypage.pageCount}" currentPage="${mypage.pageNumber}"/>
  </tr>
</table>
  </body>
</html:html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值