title: Paging with Spring MVC, JPA and Datatables
body: <p>I want to use paging in my Spring controller and in my view where I use datatables. In the controller I use a Spring data repository with paging support:</p>
  
  <pre><code>@GetMapping("my/url")
  public String listAll(
          Model model,
          @PageableDefault(size=200, sort="name") Pageable pageable)
  {
      Page&lt;MyEntity&gt; page = repository.findAll(pageable);
      model.addAttribute("page", page);
      return "path-to-html-resource";
  }
  </code></pre>
  
  <p>In my view I don't know how to use the Page object properly.</p>
  
  <pre><code>// this is how I extract the data from the controller (thymeleaf style)
  var datatableRows = /*[[${page.getContent()}]]*/'';
  
  $('#mainTable').DataTable({
      "paging" : true,
      "serverSide" : true,
      "ajax" : function (data, callback, settings) {
          // this is definitely not the way to go
          // but so far was the only way I got some data into my table
          var myObject = new Object();
          myObject.aaData = datatableRows;
          callback(myObject);
      },
      "info" : true,
      "columns": [
          { "data": "name"},
          { "data": "validFrom"},
          { "data": "validTo"},
          { "data": "amount"},
          { "data": "currency"}
      ]
  });
  </code></pre>
  
  <p>Also, how do I have to write the ajax part of the datatables definition so I can pass the Spring pageable object back and forth? And how to I wire the datatable buttons to the paging index?</p>