类库
pager-src.jar
pager-taglib.jar taglib.tld
jstl-1.2.jar c.tld
文件
vo.java
PagerModel.java
AdminFileService.java
AbstractService
AdminFileServiceImpl.java
AdminFileAction.java
index.jsp
pager_taglib.jsp
package com.bit.jsp.pager; import java.util.List; public class PagerModel { private List datas; private int total; public List getDatas() { return datas; } public void setDatas(List datas) { this.datas = datas; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } }
package com.company.service.file; import java.util.List; import com.bit.jsp.pager.PagerModel; public interface AdminFileService { /** * 获得文件分页对象 * @param offset * @param pagesize * @return */ public PagerModel findAllFiles(int offset,int pagesize); }
package com.company.service.impl.file; import java.util.List; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.bit.jsp.pager.PagerModel; public abstract class AbstractService extends HibernateDaoSupport { /** * 由Spring IOC注入 sessionFactory */ /*@Override protected HibernateTemplate createHibernateTemplate( SessionFactory sessionFactory) { // TODO Auto-generated method stub return super.createHibernateTemplate(sessionFactory); }*/ public PagerModel searchPaginated(String hql,int offset,int pagesize){ // 1.获得分页记录总数 String countHql = getCountQuery(hql); Query query = getSession().createQuery(countHql); int total = ((Long)query.uniqueResult()).intValue(); // 2.获得分页数据列表 query = getSession().createQuery(hql); query.setFirstResult(offset); query.setMaxResults(pagesize); List datas = query.list(); // 3.为分页对象设置属性值 PagerModel pm = new PagerModel(); pm.setDatas(datas); pm.setTotal(total); return pm; } // 获得记录总数查询语句 // hql = select * from ....替换成 select count(*) private String getCountQuery(String hql){ int index = hql.indexOf("from"); if(index != -1) { return "select count(*) " + hql.substring(index); } return hql; } }
package com.company.service.impl.file; import java.util.List; import java.util.regex.Pattern; import org.hibernate.Query; import org.hibernate.SQLQuery; import com.bit.jsp.pager.PagerModel; import com.company.hibernate.vo.disk.VO; import com.company.service.file.AdminFileService; public class AdminFileServiceImpl extends AbstractService implements AdminFileService { public PagerModel findAllFiles(int offset,int pagesize) { String selectHql = "from com.company.hibernate.vo.disk.VO"; return searchPaginated(selectHql,offset,pagesize); } }
package com.compay.struts.action.file; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.springframework.web.struts.DispatchActionSupport; import com.bit.jsp.pager.PagerModel; import com.company.service.file.AdminFileService; import com.company.struts.form.AdminFileSearchActionForm; public class AdminFileAction extends DispatchActionSupport{ /** * service 由 spring IOC 注入,需要提供setXxx() */ private AdminFileService adminFileService; public void setAdminFileService(AdminFileService adminFileService) { this.adminFileService = adminFileService; } /** * parameter="act", /adminFile.do */ @Override protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 1.获得表单数据 int offset = 0; if(request.getParameter("pager.offset") != null) { offset = Integer.parseInt(request.getParameter("pager.offset")); } // 2.调用业务逻辑 int pagesize = 10; PagerModel pm = adminFileService.findAllFiles(offset,pagesize); request.setAttribute("pm", pm); request.setAttribute("pagesize", pagesize); // 3.转向视图 return mapping.findForward("index"); } }
<c:forEach items="${pm.datas}" var="item"> 略! </c:forEach> <c:if test="${empty pm.datas[0] }"> 略! </c:if>
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager" %> <pg:pager url="adminFile.do" items="${pm.total}" maxPageItems="${pagesize}" export="currentPageNumber=pageNumber"> <pg:first> <a href="${pageUrl}" id="firstpageurl">首页</a> </pg:first> <pg:prev> <a href="${pageUrl }">前页</a> </pg:prev> <pg:pages> <c:choose> <c:when test="${currentPageNumber eq pageNumber}"> <font color="red">${pageNumber }</font> </c:when> <c:otherwise> <a href="${pageUrl }">${pageNumber }</a> </c:otherwise> </c:choose> </pg:pages> <pg:next> <a href="${pageUrl }">后页</a> </pg:next> <pg:last> <a href="${pageUrl }">尾页</a> </pg:last> </pg:pager>