避免重置在托管bean中重复页面边界(p:dataTable)的样板代码

Tin*_*iny 12 datatable jsf primefaces lazydatamodel

从像a这样的迭代组件中删除行时<p:dataTable>,如果删除了最后一页所有行,则需要将当前页面重置为其前一页面.这是不幸的是没有自动在<p:dataTable>LazyDataModel<T>.

语言上,如果一个数据表包含11页,每页10行,并且第11页上的所有行,即最后一行都被删除,它应该自动获得第10页(即前一页),但这不会自动发生(当前页面保持静止(第11个),就像数据表本身被清空一样),除非在关联的辅助bean中的某处显式编码.

非正式地,相应的伪代码段看起来如下所示.

if (rowCount <= (ceiling)((first + 1) / pageSize) * pageSize - pageSize) {
    first -= pageSize;
}
Run Code Online (Sandbox Code Playgroud)

first页面偏移(以...开头0)在哪里,pageSize表示每页的行数,并rowCount指示关联的数据存储/数据库中的总行数.

实际上:

@Override
public List<Entity> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, Object> filters) {

    // ...

    int rowCount = service.getRowCount();
    setRowCount(rowCount);

    // ...

    if (pageSize <= 0) {
        // Add an appropriate FacesMessage.
        return new ArrayList<Entity>();
    } else if (first >= pageSize && rowCount <= Utility.currentPage(first, pageSize) * pageSize - pageSize) {
        first -= pageSize;
    } else if (...) {
        // ...
    }

    // ...

    return service.getList(first, pageSize, map, filters);
    // SortMeta in List<SortMeta> is PrimeFaces specific.
    // Thus, in order to avoid the PrimeFaces dependency on the service layer,
    // List<SortMeta> has been turned into a LinkedHashMap<String, String>()
    // - the second last parameter (named "map") of the above method call.
}
Run Code Online (Sandbox Code Playgroud)

静态效用方法Utility#currentPage()定义如下.

public static int currentPage(int first, int pageSize) {
    return first <= 0 || pageSize <= 0 ? 1 : new BigDecimal(first + 1).divide(new BigDecimal(pageSize), 0, BigDecimal.ROUND_CEILING).intValue();
}
Run Code Online (Sandbox Code Playgroud)

这是一段样板代码,应该避免在整个地方重复 - 在每个使用<p:dataTable>with的托管bean中LazyDataModel<T>.

有没有办法自动化这个过程?