pager-taglib分页逻辑的封装处理

为了避免在Action(呈现层)和Manager(业务逻辑层)之间传递大量的参数,
可以使用ThreadLocal模式来传递分页参数(包括:offset和pagesize)。
- 定义: 参考SystemContext.java
- 往ThreadLocal中赋值:参考PagerFilter.java
- 从ThreadLocal中获取分页参数:参考AbstractManager.java


package com.xwj.oa.pageSearch;
import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.xwj.oa.utils.SystemException;



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,int offset,int pagesize){
		return  searchPaginated ( hql,null,offset,pagesize);
	}

	
/**
 * 
 * @param hql
 * @param params
 * @param offset
 * @param pagesize  
 * @return
 */
	public pagerModel searchPaginated (String hql,Object[] params,int offset,int pagesize){
		
		String countHql = this.getCountQuery(hql);
		Query query = this.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 = this.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 pm = new pagerModel();
		pm.setDatas(datas);
		pm.setTotal(total);
		
		return pm;
		
		
	}
	
	private String getCountQuery(String hql){
		int index = hql.indexOf("from");
		if(index != -1){
			String countQuery = "select count(*) "+" "+hql.substring(index);
			return countQuery;
		}
		throw new SystemException("无效的HQL查询");
	}
}



package com.xwj.oa.pageSearch;

public class SystemContext {

	private static ThreadLocal offset = new ThreadLocal();
	private static ThreadLocal pagesize = new ThreadLocal();

	public static int getOffset(){
		Integer os = (Integer)(offset.get());
		if(os == null){
			return 0;
		}
		return os.intValue();
	}
	
	public static void setOffset(int offsetValue){
			offset.set(new Integer(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.intValue();
	}
	
	public static void setPagesize(int pagesizeValue){
		pagesize.set(new Integer(pagesizeValue));
	}
	
	public static void removePagesize(){
		pagesize.remove();
	}

}



package com.xwj.oa.pageSearch;

import java.util.List;

public class pagerModel {
	/**
	 * 总记录数
	 */
	private int total;

	/**
	 * 当前的结果集
	 */
	private List datas;

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getDatas() {
		return datas;
	}

	public void setDatas(List datas) {
		this.datas = datas;
	}
}


package com.xwj.oa.pageSearch;

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;



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(this.getOffset(httpRequest));
		SystemContext.setPagesize(this.getPagesize(httpRequest));
		
		try{
		chain.doFilter(request, response);
		}finally{
			SystemContext.removeOffset();
			SystemContext.removePagesize();
		}

	}
	
	private int getOffset(HttpServletRequest httpRequest){
		int offset = 0 ;
		try{
			offset=Integer.parseInt(httpRequest.getParameter("pager.offset"));
		}catch(NumberFormatException ignore){
			
		}
		return offset;
	}
	
	private int getPagesize(HttpServletRequest httpRequest){
		
		return 10;
	}

	public void init(FilterConfig arg0) throws ServletException {

	}

}


上面写了一个fileter,因此要在web-xml中作相应的配置
<!-- pagerFileter -->
	<filter>
		<filter-name>PagerFilter</filter-name>
		<filter-class>com.xwj.oa.pageSearch.PagerFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>PagerFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>



public class OrgManagerImpl extends AbstractManager implements OrgManager {
	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);
	} 
}



public class OrgAction extends DispatchAction {

	private OrgManager orgManager;
	
	/**
	 * 打开机构管理主界面
	 */
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		OrgActionForm oaf = (OrgActionForm)form;

		request.setAttribute("pm",
			orgManager.findOrgs(oaf.getParentId())
		);		
	}	
}




<pg:pager url="org.do" items="${pm.total }" maxPageItems="10" export="currentPageNumber=pageNumber">
								<pg:param name="parentId" />
								<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>

你可能感兴趣的:(java,Hibernate,orm)