用struts+hibernate做了个分页

在daoImp中写入此两个方法方法

//用与获取一个表中总的行数

public int countRow()
 {
  Session s=getSession();
  HibernateUtil.beginTransaction();
  Query query=s.createSQLQuery("select count(*) from Category");
  int countRow=Integer.parseInt(query.uniqueResult().toString());
  HibernateUtil.commitTransaction();
  HibernateUtil.closeSession();
  
  return countRow;
 }

//获取hibernate中第offset行的到第index个值到list中

 public List getListCategorys(int offset,int index){
  Session s=getSession();
  HibernateUtil.beginTransaction();
  Query query=s.createQuery("from Category");
  query.setFirstResult(i);
  query.setMaxResults(j);
  List list=query.list();
  HibernateUtil.commitTransaction();
  HibernateUtil.closeSession();
  return list;
 }

//把list中的值放入VO中,再把VO对象放入categorylist

package org.zjut.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.zjut.DAOFactory.DAOFactory;
import org.zjut.GetSession.GetSession;
import org.zjut.PO.Category;
import org.zjut.PO.base.BaseCategory;

public class DataSources extends GetSession{
 public List getListCategory(int offset,int index)
 {
  List list=DAOFactory.getIntance().createCategoryDAO().getListCategorys(offset,indexj);
  List<BaseCategory> categorylist=new ArrayList<BaseCategory>();
  Iterator it=list.iterator();
  Category category;
  
  while(it.hasNext())
  {
   BaseCategory categoryVO=new Category();
   category=(Category)it.next();
   Long id=category.getId();
   String name=category.getName();
   
   categoryVO.setId(id);
   categoryVO.setName(name);
   categorylist.add(categoryVO);
   
   
  }
  return categorylist;
 }
}

//用于分页的action
package org.zjut.actions;

import java.io.IOException;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.zjut.DAOFactory.DAOFactory;
import org.zjut.service.DataSources;

 

public class ListCategoryAction extends Action {

 public ActionForward execute(ActionMapping mapping,
         ActionForm form,
         HttpServletRequest request,
         HttpServletResponse response)
         throws IOException, ServletException {
  request.setAttribute("pageno",null);
  request.setAttribute("totalpage", null);
  int totalno=DAOFactory.getIntance().createCategoryDAO().countRow();  
  int pageno=0;int pageSize=10;int i=0;int j=pageSize;
  int totalpage=(totalno%pageSize)==0?(totalno/pageSize):(totalno/pageSize+1);    
  if(totalno<=pageSize)
  {
   i=0;j=totalno;pageno=1;
   request.setAttribute("pageno", pageno);
   request.setAttribute("totalpage", totalpage);
  }
  else{
   String strno=(String)request.getParameter("pageno");  
   if(strno==null){
    pageno=1;i=0;j=pageSize;    
    request.setAttribute("pageno", pageno);
    request.setAttribute("totalpage", totalpage);
   }else{    
    pageno=Integer.parseInt(strno);
    if(pageno<1)
    {
     pageno=1;i=0;j=pageSize;     
     request.setAttribute("pageno", pageno);
     request.setAttribute("totalpage", totalpage);
    }
    else{
     i=(pageno-1)*pageSize;j=pageSize;
     request.setAttribute("pageno", pageno);
     request.setAttribute("totalpage", totalpage);
    }    
   }   
  }  
  List categorylist=new DataSources().getListCategory(offset,index);   
  request.setAttribute("list",categorylist); 
  String target = "display";
    
  return mapping.findForward(target);
 }
 

}

//通过action返回的pageno和allpage分页

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<style>
#idDIV{width:300px;height:120px;background-color:#000000;padding:4px;font-family:verdana,tahoma;}
#idDIV td{background-color:#FFD700;}
#idCodeDiv{width:300px;padding:4px;font-family:verdana,tahoma;margin:12px 0px 0px 0px;background-color:#EEEEEE;font-weight:bold;}
</style>

<html:html>
<head>
<title> insreat title here </title>
</head>
<body><div align="center" style="background-color:blue;">
<html:errors/>
<c:set var="pageno" value="${pageno}"></c:set>
<c:set var="totalpage" value="${totalpage}"></c:set>

<bean:message key="number"/>   <bean:message key="category"/>   <bean:message key="operat"/>
<br><%int i=1; %>
<c:forEach var="cate" items="${list}">
<c:out value="<%=i++ %>"></c:out><c:out value="${cate.name}"></c:out><bean:message key="editor"/><bean:message key="delete"/>
<BR>
</c:forEach>

<c:if test="${pageno ne 1}"><a href='<c:url value="/listCategoryAction.do"><c:param name="pageno" value="1"/></c:url>'><bean:message key="first"/></a></c:if>

<c:if test="${pageno gt 1}"><a href='<c:url value="/listCategoryAction.do"><c:param name="pageno" value="${pageno-1}"/></c:url>'><bean:message key="Previous"/></a></c:if>

<c:if test="${pageno lt totalpage}"><a href='<c:url value="/listCategoryAction.do"><c:param name="pageno" value="${pageno+1}"/></c:url>'><bean:message key="next"/></a></c:if>

<c:if test="${pageno ne totalpage}"><a href='<c:url value="/listCategoryAction.do"><c:param name="pageno" value="${totalpage}"/></c:url>'><bean:message key="last"/></a></c:if>
</div></body>
</html:html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值