. 如何分页?
1.把pager-taglib.jar放到lib目录下(仅此足亦,完全没必要改web.xml什么的,还有官方war包中的东西都可以不要)
2.在Jsp页面嵌入<%@ taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>
分页代码:
action中示范代码:
PageModel pm = empMgr.selectAllEmp(PageModel.getOffset(request), PageModel.getPageSize(request)) ;
request.setAttribute("pm", pm) ;
jsp示范代码:
........
........
//分页代码
<pg:pager url="employee.do" items="${pm.total}" maxPageItems = "5" maxIndexPages="10" export="currentPageNumber=pageNumber" >
<pg:param name="pageSize" value="5" />
<pg:first>
<a href="${pageUrl}">首页</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>
/*************PageModule类的代码**************/
public class PageModel {
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;
}
public static int getOffset(HttpServletRequest request) {
int offset = 0 ;
String strOffset = request.getParameter("pager.offset") ;
if (strOffset != null) {
offset = Integer.parseInt(strOffset) ;
}
return offset ;
}
public static int getPageSize(HttpServletRequest request) {
int pageSize = 5 ;
String strPageSize = request.getParameter("pageSize") ;
if (strPageSize != null) {
pageSize = Integer.parseInt(strPageSize) ;
}
return pageSize ;
}
}
/********************************************************************/
/*****************抽象分页*******仅供参考******************************/
/********************************************************************/
package com.oa.manager.impl;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.oa.PagerModel;
public class AbstractPagerManager extends HibernateDaoSupport {
public PagerModel searchPaginated(String hql,int offset, int pagesize) {
return this.searchPaginated(hql, null, offset, pagesize);
}
public PagerModel searchPaginated(String hql, Object obj, int offset, int pagesize) {
return this.searchPaginated(hql, new Object[]{obj}, offset, pagesize);
}
/**
* 根据Hql语言进行分页
* @param hql HQL语句
* @param params HQL语句的参数
* @param offset 从第几条记录开始查询
* @param pagesize 每页显示多少条
* @return
*/
@SuppressWarnings("unchecked")
public PagerModel searchPaginated(String hql,Object[] params, int offset, int pagesize) {
//获取总记录数
String countHql = this.getCountQuery(hql);
System.out.println("countHql: " + countHql);
Query query = this.getSession().createQuery(countHql);
if(query != null && params != null && params.length > 0) {
for(int i=0; i<params.length; i++) {
query.setParameter(i, params[i]);
}
}
int total = ((Long)query.uniqueResult()).intValue();
//获得当前页的结果集
query = this.getSession().createQuery(hql);
if(query != null && params != null && params.length > 0) {
for(int i=0; i<params.length; i++) {
query.setParameter(i, params[i]);
}
}
query.setFirstResult(offset);
query.setMaxResults(pagesize);
List<Organization> list = query.list();
PagerModel pm = new PagerModel();
pm.setTotal(total);
pm.setList(list);
return pm;
}
private String getCountQuery(String hql) {
System.out.println(hql);
int index = hql.indexOf("from");
System.out.println("index:" + index);
if(index != -1){
return "select count(*) " + hql.substring(index);
}
System.out.println("问题出了");
throw new RuntimeException("无效的HQL查询语句!");
}
}