Accessing pagination info from liferay search-cont

2019-09-06 00:57发布

I am developing a Portlet with Liferay (using liferay-ui in the JSP) and SpringMVC.

I have the following code in my JSP:

<liferay-ui:search-container delta="5" emptyResultsMessage="no books!">
  <%
    List<Book> bookList = (List<Book>)request.getAttribute("bookList");
    List<Book> bookListView = ListUtil.subList(bookList, searchContainer.getStart(), searchContainer.getEnd());
  %>
<liferay-ui:search-container-results results="<%= bookListView %>" total="${numberOfBooks}">

</liferay-ui:search-container-results>
  ...

I'd really like to get rid of the Java Code Block in the JSP and have the bookListView as model attribute just like numberOfBooks in the above code.

However, I can't find a way to access searchContainer from the Spring Controller to get the start and end of the pagination...

Any ideas? Thx!

2条回答
Evening l夕情丶
2楼-- · 2019-09-06 01:25

Create a suitable SearchContainer in your controller and add it to your model. As Prakash K already said this SearchContainer could look like this:

SearchContainer<Book> searchContainer = new SearchContainer<Book>(renderRequest, renderResponse.createRenderURL(), null, "there are no books");

Because of the two parameter renderRequest and renderResponse you can not use the @ModelAttribute annotation to add the SearchContainer as a model attribute.

Then the JSP can be written like this:

<liferay-ui:search-container searchContainer="${model.searchContainer}" delta="${model.searchContainer.delta}" deltaParam="books_delta">
    <liferay-ui:search-container-results results="${model.searchContainer.results}" total="${model.searchContainer.total}"/>    

    <liferay-ui:search-container-row
        className="Book"
        keyProperty="primaryKey"
        modelVar="book">
        ...
    </liferay-ui:search-container-row>

    <liferay-ui:search-iterator searchContainer="${model.searchContainer}"/>

</liferay-ui:search-container>

The attribute deltaParam can be used to configure the used URL parameter

查看更多
Viruses.
3楼-- · 2019-09-06 01:30

This might work for you:

SearchContainer<Book> searchContainer = new SearchContainer<Book>(renderRequest, renderResponse.createRenderURL(), null, "there are no books");

Or else,

You can get the parameters from request: delta=20 & cur=2
where cur is the current page which is requested and delta is the total number of items on a page.
With this you can calculate the start (0,20,40, ...) and end (19,39,59, ...) as does liferay's SearchContainer with this method:

private void _calculateStartAndEnd() {
    _start = (_cur - 1) * _delta;
    _end = _start + _delta;

    _resultEnd = _end;

    if (_resultEnd > _total) {
        _resultEnd = _total;
    }
}
查看更多
登录 后发表回答