首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

施用pager-taglib.jar实现的分页技术

2012-11-03 
使用pager-taglib.jar实现的分页技术//1.导入pager-taglib.jar包;??//2.引入pager-taglib标签:%@ taglibp

使用pager-taglib.jar实现的分页技术

//1.导入pager-taglib.jar包;?

?
//2.引入pager-taglib标签:<%@ taglibprefix="pg"? uri="http://jsptags.com/tags/navigation/pager"%>??
//3.建立PagerModel类:??
public class PagerModel {??
????
??? private inttotal;??
????
??? private Listdatas;??
??? public ListgetDatas() {??
???????return datas;??
???}??
??? public voidsetDatas(List datas) {??
???????this.datas = datas;??
???}??
??? public intgetTotal() {??
???????return total;??
???}??
??? public voidsetTotal(int total) {??
???????this.total = total;??
???}??
}??
//4.pager业务逻辑部分:??
public class AbstractManager extends HibernateDaoSupport{??
??? publicPagerModel searchHql(Stringhql){??
???????returnsearchHql(hql,null,SystemContext.getOffset(),SystemContext.getPageSize());??
???}??
??? publicPagerModel searchHql(String hql,Objectdatas){??
???????return searchHql(hql,newObject[]{datas},SystemContext.getOffset(),SystemContext.getPageSize());??
???}??
??? publicPagerModel searchHql(String hql,Object[]datas){??
???????returnsearchHql(hql,datas,SystemContext.getOffset(),SystemContext.getPageSize());??
???}??
??? publicPagerModel searchHql(String hql,int offset,intpageSize){??
???????returnsearchHql(hql,null,offset,pageSize);??
???}??
??? publicPagerModel searchHql(String hql,Object datas,int offset,intpageSize){??
???????return searchHql(hql,newObject[]{datas},offset,pageSize);??
???}??
??? publicPagerModel searchHql(String hql,Object[] datas,int offset,intpageSize){??
???????String countHql =getQuery(hql);??
???????Query query =getSession().createQuery(countHql);??
???????if(datas !=null && datas.length> 0){??
???????????for(int i = 0 ;i<datas.length;i++)??
???????????????query.setParameter(i,datas[i]);??
???????}??
???????int total =((Long)query.uniqueResult()).intValue();??
???????Queryquery1=getSession().createQuery(hql);??
???????if(datas !=null && datas.length> 0){??
???????????for(int i = 0 ;i<datas.length;i++)??
???????????????query1.setParameter(i,datas[i]);??
???????}??
???????List list=query1.setFirstResult(offset)???????????????????????????????????????????.setMaxResults(pageSize).list();??
???????PagerModel pm = newPagerModel();??
???????pm.setDatas(list);??
???????pm.setTotal(total);??
???????return pm;??
???}??
??? publicString getQuery(Stringhql){??
???????int index=hql.indexOf("from");??
???????if(index != -1){??
???????????return "selectcount(*)"+hql.substring(index);??
???????}??
???????throw newSystemException("查询语句出错!");??
???}??
}??
?
//5.定义offset、pagesize类:??
public class SystemContext{??
??? privatestatic ThreadLocal offset = newThreadLocal();??
??? privatestatic ThreadLocal pageSize = newThreadLocal();??
??? publicstatic int getOffset(){??
???????Integer os =(Integer)offset.get();??
???????if(os == null){??
???????????return 0;??
???????}??
???????return os;??
???}??
??? publicstatic void setOffset(intoffsetvalue){??
???????offset.set(offsetvalue);??
???}??
??? publicstatic voidremoveOffset(){??
???????offset.remove();??
???}??
??? publicstatic int getPageSize(){??
???????Integer ps =(Integer)pageSize.get();??
???????if(ps == null){??
???????????return Integer.MAX_VALUE;??
???????}??
???????return ps;??
???}??
??? publicstatic void setPageSize(intpageSizevalue){??
???????pageSize.set(pageSizevalue);??
???}??
??? publicstatic voidremovePageSize(){??
???????pageSize.remove();??
???}??
}??
//6.过滤器中初始化pagesize、offset:??
public class PagerFilter implements Filter{??
??? public voiddestroy() {??
???}??
??? public voiddoFilter(ServletRequest request, ServletResponseresponse,??
???????????FilterChain chain) throws IOException, ServletException{??
???????HttpServletRequest httpRequest=(HttpServletRequest)request;??
???????SystemContext.setOffset(getOffset(httpRequest));??
???????SystemContext.setPageSize(getPageSize(httpRequest));??
???????try{??
???????????chain.doFilter(request,response);??
???????}finally{??
???????????//清空ThreadLocal中的值??
???????????SystemContext.removeOffset();??
???????????SystemContext.removePageSize();??
???????}?????????????
???}??
??? public voidinit(FilterConfig arg0) throws ServletException{??
???}??
??? public intgetOffset(HttpServletRequestrequest){??
???????int offset = 0;??
???????try {??
???????????offset =Integer.parseInt(request.getParameter("pager.offset"));??
???????} catch (NumberFormatException ignore){??
???????}??
???????return offset;??
???}?????
???????//设置每页显示多少条记录??
??? public intgetPageSize(HttpServletRequestrequest){??
???????return 3;??
???}??
}??
//7.配置过滤器web.xml中:??
<filter>??
???<filter-name>pager-taglib</filter-name>??
???<filter-class>com.oa.util.filter.PagerFilter</filter-class>??
</filter>??
?
<filter-mapping>??
???<filter-name>pager-taglib</filter-name>??
???<url-pattern>/*</url-pattern>??
</filter-mapping>??
//8.显示效果:??
<pg:pager url="org.do" items="${pm.total }"export="currentPageNumber=pageNumber">??
???<pg:paramname="parentId"/>??
???<pg:first>??
???????<a href="${pageUrl}"mce_href="${pageUrl}">首页</a>??
???</pg:first>??
???<pg:prev>??
???????<a href="${pageUrl }" mce_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 }" mce_href="${pageUrl}">${pageNumber}</a>??
???????????</c:otherwise>??
???????</c:choose>??
???</pg:pages>??
???<pg:next>??
???????<a href="${pageUrl }" mce_href="${pageUrl}">后页</a>??
???</pg:next>??
???<pg:last>??
???????<a href="${pageUrl }" mce_href="${pageUrl}">尾页</a>??
???</pg:last>??
</pg:pager>?

热点排行