Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Data JPA tables with search, pagination and sorting

I'm developing an adminstration interface for a set of tables. I need to implement functionalities such as listing, sorting, filtering and pagination.

I'm using Spring Boot as a starter and Spring Data Jpa for repository. I've searched the Web for some examples about a complete solution that includes all the above functionalities. What I found included almost all of them, but appearently if there was pagination and sorting there wasn't filtering or viceversa.

Now I'm storing the filter in a application object on session using @ModelAttribute but I know that is now a good design because the applciation will extend and it becomes hard to maintain. I'm also using Page and Pagination for pagination purpose and using Specifications for filtering.

What I want is to submit all the data, i.e.: search fields, sorted fields and current page, in a single request. Off course if the search fields are not empty the pagination will be reinitialize.

Another thing is that I don't want to use jQuery datatables but plain HTML and form submission.

Here are some tutorials and examples that I found:

Link 1

Link 2

Thanks in advance

EDIT html form included

Here is the structure of my table and my pagination section:

<form method=post action=someLink>
    <table> -populated from controller using Thymeleaf - </table>
    <div class=pagination>
         <ul>  - actually this div is build using the page object returned from server -
            <li><a href=link/?page=;size=;>1</a></li>
            <li><a href=link/?page=;size=;>2</a></li>
            <li><a href=link/?page=;size=;>3</a></li>
         </ul>
    </div>
</form>

As you can see the form is separated from my pagination div. When I click on a page number a get request is send to the server and executes the query with the stored filter. When I submit the form the page number is not taken into consideration because the number of pages can change.

So my question is how to build the form to include the pagination in one single submit.

I'm thinking instead of using a's to use input elements so on the server I can read the data from them. I don't know how to submit that post request with the pageable attributes.

Thanks

like image 976
bogdan.rusu Avatar asked Oct 02 '15 09:10

bogdan.rusu


1 Answers

The first thing that you have to do is to enable Spring Data web support. If you are using Spring Boot, it's probably activated by Spring Boot.

After you have enabled the Spring Data web support, you can specify the current page, page size, and sorting options by settings the values of these request parameters:

  • The value of the page request parameter identifies the current page (this is zero indexed).
  • The value of the size request parameter defines the page size. The default page size is 20.
  • The value of the sort request parameter defines the sort options. Spring Data JPA reference manual provides more information about the supported syntax.

You can now "inject" the requested page information into your controller method by adding a new org.springframework.data.domain.Pageable method parameter into your controller method. In your case, the controller method could look as follows:

@RequestMapping(value = "/search", method=RequestMethod.POST)
public String search(@ModelAttribute("searchFilter") FilterDTO filter, Pageable page) {
    //Add logic here
    return "results";
}
like image 178
pkainulainen Avatar answered Nov 19 '22 22:11

pkainulainen