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

首先,需要在ec:table标签中设置
[color=green]filterable="true"
filterRowsCallback="limit"
retrieveRowsCallback="limit"
sortRowsCallback="limit"[/color]

在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();
}

如此便可实现基本的功能。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值