What is the right way to paginate (especially with jQuery and jersey)

I just want to run my pagination method using Jersey on the server side and JQuery on the client.

On the server (assuming the user has just requested / left / users? Page = 1):

// Hardcoded the page number here, but it would be page-1, page+1 String prevLink = uriInfo.getRequestUri() + "?page=0"; String nextLink = uriInfo.getRequestUri() + "?page=2"; String linkHeader = String.format(LINK_HEADER_TMPL, prevLink, nextLink); return List<User> currentPageResults; 

This gives me the following answer in response:

 Link: <http://localhost:8880/rest/users?page=0>; rel="prev", <http://localhost:8880/rest/users?page=2>; rel="next" 

On the client:

  var xhr = $.getJSON("rest/users", function(allData) { var links = (xhr.getResponseHeader("link")).split(','); var relLinks = []; $.each(links, function(index, value) { var parts = value.split(';'); var url = parts[0].replace(/^.*<(.*)>.*$/m, '$1'); var rel = parts[1].replace(/^.*"(.*)".*$/m, '$1'); relLinks[rel] = url; }); alert(relLinks['prev']); 

The warning gives me the correct information, and this is how I will use it to create links to pages:

 http://localhost:8880/rest/users?page=0 

So the questions are:

  • Is my link title correctly formatted? Parsing the client side link header seems a bit overwhelmed.
  • Is this the correct / accepted way to do this? I looked at the Jersey response filter, but this requires adding Jersey-specific annotations to the model class, which I did not want to do.
  • What about the common pages available? Is there a standardized way of giving this in information? If I had a shared page, could I supposedly completely remove the rel links and just generate them on the client side? Is it correct?

By the way, how did the whole format of link headers appear? The form "url; rel = name, url; rel = name" seems a little strange to me.

EDIT: Found this http://tools.ietf.org/html/rfc5988#section-6.2 that indicates the "standard" relationship names

+6
source share
1 answer

Assuming you want these pages to be search engine friendly, I would recommend changing your links to remove the query string.

 http://localhost:8880/rest/users/0 

where 0 is processed on the server side to the desired page number. As for shared pages, you will find out on the server if you are at the end of the list of users. If so, just drop your next link. Similarly, if currentPage == 0, then omit prevLink.

You can see it in action here: http://www.autoquoter.com/aq/en/Blog/Archives

0
source

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


All Articles