`
aiaiya
  • 浏览: 41699 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
社区版块
存档分类
最新评论

结合SpringSide与extremecomponents的数据库分页、排序和过滤

    博客分类:
  • java
阅读更多
首先,需要在ec:table标签中设置
filterable="true"
filterRowsCallback="limit"
retrieveRowsCallback="limit"
sortRowsCallback="limit"


在StrutsEntityAction类添加一个方法
public ActionForward doQuery(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
	T obcect = null;
	try {
		obcect = entityClass.newInstance();
	} catch (InstantiationException e) {
		log.error(e);
	} catch (IllegalAccessException e) {
		log.error(e);
	}
	bindEntity(form, obcect);

	// 当列表的分页 过滤 排序等操作是基于数据库时,必须要用到Limit对象.
	// 注意,当页面有多个ec的时候,需要使用带 tableId参数的同名方法.
	Limit limit = ExtremeTablePage.getLimit(request);
	// 基于数据库的排序.
	// ec会取得排序的相关信息:当前是按哪个(目前只支持单列排序)column排序的,以及排序的方式desc或asc,
	Sort sort = limit.getSort();
	Map<String, String> sortValueMap = sort.getSortValueMap();  //此方法可自行封装
	if (sortValueMap.isEmpty()) {
		sortValueMap.put(idName, TableConstants.SORT_ASC);
	}
	// 基于数据库过滤.
	// ec会取得过滤的相关信息:当前是对哪些column进行过滤,以及过滤的内容是什么
	FilterSet filterSet = limit.getFilterSet();
	Map<String, String> filterPropertyMap = filterSet.getPropertyValueMap();  //此方法可自行封装
	// DEFAULT_PAGE_SIZE ==0 时, 每页记录数会使用 properties文件内的默认设置
	// DEFAULT_PAGE_SIZE <0 时, 每页记录数会等于全部记录数
	int totalRows = ExtremeTablePage.getTotalRowsFromRequest(request);
	if (totalRows < 0) {
		// 取得记录总条数时,不要忘了把filter作为参数传入,因为要取得的总行数也是要接受条件限制的.
		totalRows = getEntityManager().FindTotalEntitys(obcect, filterPropertyMap);
	}
	limit.setRowAttributes(totalRows, DEFAULT_PAGE_SIZE);
	//保存总计行数到返回
	request.setAttribute(TableConstants.TOTAL_ROWS, totalRows);
	// 取得当前要查询的页面的记录起止行号
	// offset表示数据编号的起始号,不同的数据库起始号不一样
	int offset = 0;
	int[] rowStartEnd = new int[] { limit.getRowStart() + offset, limit.getRowEnd() + offset };
	// rowStartEnd[0], rowStartEnd[1] 左闭 右开
	List list = (List) getEntityManager().FindEntitysForPage(rowStartEnd[0], rowStartEnd[1], obcect, sortValueMap, filterPropertyMap);

	request.setAttribute(getEntityListName(), list);
	return mapping.findForward(LIST);
}

其中应用到ExtremeTablePage类
/**
 * ec组件的工具类
 * 
 * @author peter
 * 
 */
public class ExtremeTablePage {
	public static Limit getLimit(HttpServletRequest request) {
		Context context = new HttpServletRequestContext(request);
		LimitFactory limitFactory = new TableLimitFactory(context);
		TableLimit limit = new TableLimit(limitFactory);
		return limit;
	}

	public static String getTableId(HttpServletRequest request) {
		return getTableId(request, null);
	}

	public static int getTotalRowsFromRequest(HttpServletRequest request) {
		return getTotalRowsFromRequest(request, null);
	}

	public static int getTotalRowsFromRequest(HttpServletRequest request, String cTableId) {
		int totalRows = -1;
		String tableId = getTableId(request, cTableId);
		tableId = tableId == null ? "" : tableId + "_";
		Integer totalRowsI = (Integer) request.getAttribute(tableId + TableConstants.TOTAL_ROWS);
		try {
			if (totalRowsI != null) {
				totalRows = totalRowsI.intValue();
			} else {
				String totalRowsS = request.getParameter(tableId + "totalrows");
				totalRows = Integer.parseInt(totalRowsS);
			}
		} catch (Exception e) {
			totalRows = -1;
		}
		totalRows = totalRows < 0 ? -1 : totalRows;
		return totalRows;
	}

	public static String getTableId(HttpServletRequest request, String cTableId) {
		if (cTableId != null) {
			return cTableId;
		}
		String tableId = request.getParameter(TableConstants.EXTREME_COMPONENTS_INSTANCE);
		if (tableId == null) {
			tableId = TableConstants.EXTREME_COMPONENTS;
		}
		return tableId;
	}
}


在HibernateEntityDao类添加两个方法
public Integer FindTotalEntitys(T object, Map<String, String> filter) {
	Criteria criteria = createCriteria();
	if (filter != null && !filter.isEmpty()) {
		Set<String> filterNameSet = filter.keySet();
		for (String name : filterNameSet) {
			criteria.add(Restrictions.like(name, "%" + filter.get(name) + "%"));
		}
	}
	return (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
}

public List<T> FindEntitysForPage(int startRow, int countPerPage, T object, Map<String, String> order, Map<String, String> filter) {
	Criteria criteria = createCriteria();
	if (order != null && !order.isEmpty()) {
		Set<String> orderNameSet = order.keySet();
		for (String name : orderNameSet) {
			if ("asc".equalsIgnoreCase(order.get(name))) {
				criteria.addOrder(Order.asc(name));
			} else {
				criteria.addOrder(Order.desc(name));
			}
		}
	}
	if (filter != null && !filter.isEmpty()) {
		Set<String> filterNameSet = filter.keySet();
		for (String name : filterNameSet) {
			criteria.add(Restrictions.like(name, "%" + filter.get(name) + "%"));
		}
	}
	return criteria.setFirstResult(startRow).setMaxResults(countPerPage - startRow).list();
}

如此便可实现基本的功能。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics