title: Paginating Query Results with Search Criteria
body: <p>I am having trouble figuring out how to switch between pages of paginated search results while maintaining the search criteria used to find the results. I have a single page with a form with input fields for several search criteria and a button to retrieve the query results based on the provided criteria. Below the form is a table to display the results and a pagination bar to choose which page of data to display in the table. Requesting the data by page number and size with the criteria is working perfectly and the pagination bar also already works. They only issue is that the search criteria is passed to the controller from the form using a submit button and then when clicking on the pagination bar links to switch pages, the search criteria is being passed to the controller empty since the form is not resubmitted when hitting the same controller method from the pagination link. Basically, whenever I use a pagination link, I do switch pages, but all of the search criteria are lost so I end up paginating through all of the results without the search criteria applied. How can I keep the search criteria while switching pages with my pagination links?</p>
  
  <p>Thymeleaf Pagination Bar:</p>
  
  <pre><code>&lt;!-- Pagination Bar --&gt;
          &lt;div th:fragment='paginationbar' th:if="${results}"&gt;
              &lt;div&gt;
                  &lt;ul class='pagination pagination-centered'&gt;
                      &lt;li th:class="${page.firstPage}?'disabled':''"&gt;
                      &lt;span th:if='${page.firstPage}'&gt;← First&lt;/span&gt;
                      &lt;a th:if='${not page.firstPage}' th:href='@{${page.url}(page=0,size=${page.size})}'&gt;← First&lt;/a&gt;
                      &lt;/li&gt;
                      &lt;li th:class="${page.hasPreviousPage}? '' : 'disabled'"&gt;
                      &lt;span th:if='${not page.hasPreviousPage}'&gt;«&lt;/span&gt;
                      &lt;a th:if='${page.hasPreviousPage}' th:href='@{${page.url}(page=${page.currentNumber-2},size=${page.size})}' title='Go to previous page'&gt;«&lt;/a&gt;
                      &lt;/li&gt;
                      &lt;li th:each='item : ${page.items}' th:class="${item.current}? 'active' : ''"&gt;
                      &lt;span th:if='${item.current}' th:text='${item.number}'&gt;1&lt;/span&gt;
                      &lt;a th:if='${not item.current}' th:href='@{${page.url}(page=${item.number-1},size=${page.size})}'&gt;&lt;span th:text='${item.number}'&gt;1&lt;/span&gt;&lt;/a&gt;
                      &lt;/li&gt;
                      &lt;li th:class="${page.hasNextPage}? '' : 'disabled'"&gt;
                      &lt;span th:if='${not page.hasNextPage}'&gt;»&lt;/span&gt;
                      &lt;a th:if='${page.hasNextPage}' th:href='@{${page.url}(page=${page.currentNumber},size=${page.size})}' title='Go to next page'&gt;»&lt;/a&gt;
                      &lt;/li&gt;
                      &lt;li th:class="${page.lastPage}? 'disabled' : ''"&gt;
                      &lt;span th:if='${page.lastPage}'&gt;Last →&lt;/span&gt;
                      &lt;a th:if='${not page.lastPage}' th:href='@{${page.url}(page=${page.totalPages - 1},size=${page.size})}'&gt;Last →&lt;/a&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>Controller Method:</p>
  
  <pre><code>@RequestMapping(value="/users/search", method=RequestMethod.GET)
  public String search(Model model, @ModelAttribute("criteria") UserSearchCriteria criteria, 
          @RequestParam(name="page", required=false, defaultValue="0") Integer pageNumber, @RequestParam(name="size", required=false, defaultValue="10") Integer pageSize) {
  
      Page&lt;User&gt; results = userService.findUsers(criteria, new PageRequest(pageNumber, pageSize));
      PageWrapper&lt;User&gt; page = new PageWrapper&lt;User&gt;(results, "/users/search");
      model.addAttribute("results", results);
      model.addAttribute("page", page);
      return "users";
  }
  </code></pre>
  
  <p>Screenshot of Search Page with Results: <a href="https://i.stack.imgur.com/njJ3P.png" rel="nofollow noreferrer">https://i.stack.imgur.com/njJ3P.png</a></p>