第一步:写一个页页模形类
package com.zhongbin.oa.pagination;
import java.util.List;
public class PageModel {
/*
* 总页数
*/
private int allCount;
/*
* 返回的当前显示页内容
*/
private List dates;
public int getAllCount() {
return allCount;
}
public void setAllCount(int allCount) {
this.allCount = allCount;
}
public List getDates() {
return dates;
}
public void setDates(List dates) {
this.dates = dates;
}
}
第二步:写一个THREADLOCAL类处理分页参数
package com.zhongbin.oa.pagination;
/*
* 处理分页时的参数,将分页参数放置于ThreadLocal中
*/
public class SystemContext {
/*
* 当前页分页开始的记录数
*/
private static ThreadLocal ffSet = new ThreadLocal();
/*
* 每页分页的记录条数
*/
private static ThreadLocal pageSize = new ThreadLocal();
public static void setOffSet(int offSetValue){
offSet.set(offSetValue);
}
public static void setPageSize(int pageSizeValue){
pageSize.set(pageSizeValue);
}
public static int getOffSet(){
int f = 0;
try {
of = Integer.parseInt(offSet.get().toString());
} catch (NumberFormatException e) {
of = 0;
}
return of;
}
public static int getPageSize(){
int ps = 0;
try {
ps = Integer.parseInt(pageSize.get().toString());
} catch (NumberFormatException e) {
ps=Integer.MAX_VALUE;
}
return ps;
}
public static void removeOffSet(){
offSet.remove();
}
public static void removePageSize(){
pageSize.remove();
}
}
第三步:写一个FILTER保存参数
package com.zhongbin.oa.filter;
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.zhongbin.oa.pagination.SystemContext;
public class PaginationFilter implements Filter{
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
//设置分页参数
SystemContext.setOffSet(getOffSet(request));
SystemContext.setPageSize(getPageSize(request));
//继续后面的工作
chain.doFilter(arg0, arg1);
//完成工作后移除参数
SystemContext.removeOffSet();
SystemContext.removePageSize();
}
private int getOffSet(HttpServletRequest request){
int ffSet = 0;
String str = request.getParameter("pager.offset");
try {
offSet = Integer.parseInt(str);
}catch(NumberFormatException e){
offSet = 0;
}
return offSet ;
}
private int getPageSize(HttpServletRequest request){
return 10;
/* int pageSize = 0;
String str = request.getParameter("pagesize");
try{
pageSize = Integer.parseInt(str);
}finally{
pageSize = Integer.MAX_VALUE;
}
return pageSize;*/
}
public void init(FilterConfig arg0) throws ServletException {
}
}
第四步:在WEB.XML中配置FILTER
<!-- 设置分面参数过虑器 -->
paginationFilter
com.zhongbin.oa.filter.PaginationFilter
paginationFilter
/*
第五步,写一个ABSTRACTSERVICES类实现分页的方法及获取FILTER保存的参数
package com.zhongbin.oa.services;
import java.util.List;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.zhongbin.oa.exception.SystemException;
import com.zhongbin.oa.pagination.PageModel;
import com.zhongbin.oa.pagination.SystemContext;
/*
* 实现了分页功能的父类,有需要实现分页功能的SERVICE类可以直接继承此类获得分面功能
*
*/
public class AbstractServices extends HibernateDaoSupport {
/*
* 跟据一条HQL 语句 返回分页数据
*/
public PageModel searchPaginated(String hql ){
return searchPaginated(hql,null);
}
/*
* 跟据一条HQL 语句 与 一个参数 返回分页数据
*/
public PageModel searchPaginated(String hql ,Object param){
return searchPaginated(hql,new Object[]{param});
}
/*
* 跟据一条HQL 语句与一个参数数组返回分页数据
*/
public PageModel searchPaginated(String hql ,Object[] params){
//用THREADLOCAL 中获取分页参数
int ffset = SystemContext.getOffSet();
int pagesize = SystemContext.getPageSize();
//待设置的总页数
int allCount = 0;
//待设置的返回数据
List dates = null;
//获取返回总页数的HQL
String allCountHql = this.getAllCountHQL(hql);
Query queryAllCount = this.getSession().createQuery(allCountHql);
//设置参数
if(params != null && params.length>0){
for (int i = 0; i < params.length; i++) {
queryAllCount.setParameter(i, params[i]);
}
}
allCount = ((Long)queryAllCount.uniqueResult()).intValue();
//查询出要返回的数据
Query queryDates = this.getSession().createQuery(hql);
if(params != null && params.length>0){
for (int i = 0; i < params.length; i++) {
queryDates.setParameter(i, params[i]);
}
}
System.out.println("offset : " + offset + "pagesize : " + pagesize);
dates = queryDates.setFirstResult(offset).setMaxResults(pagesize).list();
PageModel pm = new PageModel();
pm.setAllCount(allCount);
pm.setDates(dates);
//返回数据
return pm;
}
//设置返回总记录数的HQL
private String getAllCountHQL(String hql){
int index = hql.indexOf("FROM");
String allCountHQL = null;
if(index != -1){
allCountHQL = "SELECT COUNT(*) " + hql.substring(index);
}else{
throw new SystemException("非法的HQL语句","exception.hql.001",hql);
}
return allCountHQL;
}
}
第六步:写一个SERVICES 继承 ABSTRACTSERVICES 调用它的方法获取分面数据:
package com.zhongbin.oa.services.impl
import com.zhongbin.oa.exception.SystemException;
import com.zhongbin.oa.pagination.PageModel;
import com.zhongbin.oa.pojo.Orgnization;
import com.zhongbin.oa.services.AbstractServices;
import com.zhongbin.oa.services.OrgServices;
public class OrgServicesImpl extends AbstractServices implements OrgServices {
/*
* 跟据PID查询所有子部门
* @see com.zhongbin.oa.services.OrgServices#findOrgs(int)
*/
public PageModel findOrgs(int parentId) {
if(parentId == 0){
return super.searchPaginated("FROM Orgnization as o where o.parent is null");
}else {
return super.searchPaginated("FROM Orgnization as o where o.parent.id = ?",parentId);
}
}
}
第七步:在ACTION 中调用此服务类的方法获取数据放于REQUEST中,在页面用于下方式显示:
<!-- 在列表数据为空的时候,要显示的提示信息 -->
<!--orgPM.dates 你设置在REQUEST的分页数据-->
class="TableBody1" nmouseover="this.bgColor = '#DEE7FF';"
οnmοuseοut="this.bgColor='#EFF3F7';">
没有找到相应的记录
background=images/list_middle.jpg> <!-- 分页代码开始 --> export="currentPageNumber=pageNumber"> <!--你的URL中的其它参数--> <!--你的URL中的其它参数--> 首页 前页 ${pageNumber } ${pageNumber } 后页 尾页 <!-- 分页代码结束 -->
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13925369/viewspace-462944/,如需转载,请注明出处,否则将追究法律责任。
上一篇:
pager-taglib分页组件的使用
下一篇:
input text
![]()
请登录后发表评论
登录
全部评论
<%=items[i].createtime%>
<%=items[i].content%> <%if(items[i].items.items.length) { %>
<%for(var j=0;j
<%}%> <%}%>
<%=items[i].items.items[j].createtime%>
<%=items[i].items.items[j].username%> 回复 <%=items[i].items.items[j].tousername%>: <%=items[i].items.items[j].content%>
还有<%=items[i].items.total-5%>条评论
) data-count=1 data-flag=true>点击查看
<%}%>
|
转载于:http://blog.itpub.net/13925369/viewspace-462944/