Access pagination information from a liferay search container in a SpringMVC controller

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

My JSP has the following code:

<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 would really like to get rid of the Java code block in the JSP and have the bookListView attribute as the model attribute, like the numberOfBooks in the above code.

However, I cannot find a way to access the search container from the Spring controller to get the start and end of pagination ...

Any ideas? thanks!

0
source share
2 answers

This might work for you:

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

Or else

You can get parameters by request: delta=20 and cur=2
where cur is the requested current page and delta is the total number of elements on the page.
With this, you can calculate the start (0,20,40, ...) and the end (19,39,59, ...), like liferay SearchContainer using this method:

 private void _calculateStartAndEnd() { _start = (_cur - 1) * _delta; _end = _start + _delta; _resultEnd = _end; if (_resultEnd > _total) { _resultEnd = _total; } } 
+1
source

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

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

Due to the two parameters renderRequest and renderResponse, you cannot use the @ModelAttribute annotation to add SearchContainer as a model attribute.

Then the JSP can be written as follows:

 <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 deltaParam attribute can be used to configure the URL parameter used.

+1
source

Source: https://habr.com/ru/post/950523/


All Articles