PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘。目前又不当COO,还是得用心记代码哦!
在上篇文章Pager-taglib分页 对其有了一个基本的懂得,接下来我们将会对pager-taglib如何在项目中应用进行一个比拟详细的说明.
1建立LocalThread类,对分页的常数进行封装
代码如下
package com.tgb.oa; /** * * 采取ThreadLocal来存储分页变量. * @author jnqqls * @group TGB * @version 1.0 * @comments */ public class SystemContext { private static ThreadLocal offset = new ThreadLocal(); private static ThreadLocal pagesize = new ThreadLocal(); /** * 从线程中获得offset字段 * @return */ public static int getOffset(){ Integer os = (Integer)offset.get(); if(os == null){ return 0; } return os; } public static void setOffset(int offsetvalue){ offset.set(offsetvalue); } public static void removeOffset(){ offset.remove(); } public static int getPagesize(){ Integer ps = (Integer)pagesize.get(); if(ps == null){ return Integer.MAX_VALUE; } return ps; } public static void setPagesize(int pagesizevalue){ pagesize.set(pagesizevalue); } public static void removePagesize(){ pagesize.remove(); } }
2建立PagerFilter过滤器,将参数读取到ThreadLocal中.
代码如下:
package com.tgb.oa.web; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import com.tgb.oa.SystemContext; /** * * @oa_02 过滤器,获得相关分页数据. * @author jnqqls * @group TGB * @version 1.0 * @comments */ public class PagerFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, 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(); } } protected int getOffset(HttpServletRequest request){ int offset = 0; try { offset = Integer.parseInt(request.getParameter("pager.offset")); } catch (NumberFormatException ignore) { } return offset; } protected int getPagesize(HttpServletRequest request){ return 10; } public void init(FilterConfig arg0) throws ServletException { } }
3配置filter在WEB.XML文件中
代码如下:
<!-- 定义分页Filter --> <filter> <filter-name>PagerFilter</filter-name> <filter-class>com.tgb.oa.web.PagerFilter</filter-class> </filter> <filter-mapping> <filter-name>PagerFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4建立抽象类,将分页功能抽象成一个计划,可以在更多的场合下进行运用.
代码如下:
package com.tgb.oa.manager.impl; import java.util.List; import org.hibernate.Query; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.tgb.oa.PagerModel; import com.tgb.oa.SystemContext; import com.tgb.oa.manager.SystemException; /** * * 抽象分页查询. * @author jnqqls * @group TGB * @version 1.0 * @comments */ public class AbstractManager extends HibernateDaoSupport { public PagerModel searchPaginated(String hql){ return searchPaginated(hql,null,SystemContext.getOffset(),SystemContext.getPagesize()); } public PagerModel searchPaginated(String hql,Object param){ return searchPaginated(hql,new Object[]{param},SystemContext.getOffset(),SystemContext.getPagesize()); } public PagerModel searchPaginated(String hql,Object[] params){ return searchPaginated(hql,params,SystemContext.getOffset(),SystemContext.getPagesize()); } /** * 没有参数 * @param hql * @param offset * @param pagesize * @return */ public PagerModel searchPaginated(String hql,int offset,int pagesize){ return searchPaginated(hql,null,offset,pagesize); } /** * 只有一个参数 * @param hql * @param obj * @param offset * @param pagesize * @return */ public PagerModel searchPaginated(String hql,Object obj,int offset,int pagesize){ return searchPaginated(hql,new Object[]{obj},offset,pagesize); } /** * 根据hql语句进行分页查询 * @param hql * @param params HQL语句带的多个参数值. * @param offset 从第几条记录开始查询 * @param pagesize 总共要显示的页数. * @return */ public PagerModel searchPaginated(String hql,Object[] params,int offset,int pagesize){ //第一步:获得总记录数. //获得总数的查询语句 String countHql=getCountQuery(hql); //创建查询对象. Query query =getSession().createQuery(countHql); //通过循环将参数设置到语句中. if(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 = getSession().createQuery(hql); if(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 datas=query.list(); //创建PagerModel对象. PagerModel pm = new PagerModel(); pm.setDatas(datas); pm.setTotal(total); return pm; } /** * 根据HQL语句,获得查找总记录数的HQL语句. * @param hql * @return */ private String getCountQuery(String hql) { //根据from字截取查询语句. int index = hql.indexOf("from"); if(index != -1){ return "select count(*) " + hql.substring(index); } throw new SystemException("无效的HQL查询语句!"); } }
5实现抽象类,完成方法的调用.
代码如下:
package com.tgb.oa.manager.impl; import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import com.tgb.oa.PagerModel; import com.tgb.oa.manager.OrgManager; import com.tgb.oa.model.Orgnization; /** * * @ 实现组织机构管理接口 * @author jnqqls * @group TGB * @version 1.0 * @comments */ public class OrgManagerImpl extends AbstractManager implements OrgManager { @Override public PagerModel findOrgs(int parentId) { //如果parentId=0,则查找顶级机构列表 if(parentId == 0){ return searchPaginated("from Orgnization o where o.parent is null"); } return searchPaginated("from Orgnization o where o.parent.id = ?", parentId); } }
剩下的就是相关的 Action进行调用,配置jsp页面.以上便是pager-taglib在项目中后台的相关应用.
总结:pager-taglib是分页的一个好工具,经过简单的配置可以轻松实现分页功能,我们所需要学习的是它本身所具有封装思想.
文章结束给大家分享下程序员的一些笑话语录: 乔布斯:怎么样还是咱安全吧!黑客:你的浏览器支持国内网银吗?苹果可以玩国内的网游吗乔布斯:......不可以黑客:那我研究你的漏洞干嘛,我也需要买奶粉!
--------------------------------- 原创文章 By
分页和代码
---------------------------------