2

I tried to implement pagination in google app engine (Java), but I am not able to achieve. It is working only forward pagination and reverse pagination is not able to achieved.

I tried storing the previous cursor value through HTTP request as below:

JSP file:

<a href='/myServlet?previousCursor=${previousCursor}'>Previous page</a>
<a href='/myServlet?nextCursor=${nextCursor}'>Next page</a>

Servlet file:

    String previousCursor = req.getParameter("previousCursor");
    String nextCursor = req.getParameter("nextCursor");
    String startCursor = null;
    if(previousCursor != null){
        startCursor = previousCursor;
    } 

    if(nextCursor != null){
        startCursor = nextCursor;
    }

     int pageSize = 3;
     FetchOptions fetchOptions = FetchOptions.Builder.withLimit(pageSize);

     if (startCursor != null) {
         fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor));
      }

        Query q = new Query("MyQuery");
        PreparedQuery pq = datastore.prepare(q);
        QueryResultList<Entity> results = pq.asQueryResultList(fetchOptions);

        for (Entity entity : results) {
          //Get the properties from the entity
        }
         String endCursor = results.getCursor().toWebSafeString(); 
        req.setAttribute("previousCursor", startCursor);
        req.setAttribute("nextCursor", endCursor);

With this I am able to retain the previous cursor value, but unfortunately the previous cursor seems to be invalid.

I also tried using reverse() method, but it is of no use. It work same as forward.

So is the any way to implement proper pagination (forward and backword) in google app engine (Java)?

I found similar one that was posted in 2010. Here also the answer was to use Cursor. But as I shown above it is not working. Pagination in Google App Engine with Java

Community
  • 1
  • 1
Angom
  • 721
  • 1
  • 11
  • 27
  • The other answer is using Python, but the idea remains the same. – Lipis Jan 03 '15 at 23:25
  • @Lipis, same idea does not imply "duplicate Q" -- a Java programmer unfamiliar with Python would just have too hard a time translating that code to his/her favorite language, so this Q should be left alone (and answered, but my Java's not quite good enough for that:-). – Alex Martelli Jan 04 '15 at 16:52
  • @AlexMartelli fair enough... I deleted the duplicate question link and yes.. my Java is also not the strongest one in this area to help further :) (P.S. Welcome back to SO.. we missed ya :) – Lipis Jan 04 '15 at 18:38
  • 1
    Similar problem but solved in a Python way could be found here: http://stackoverflow.com/questions/17975070/how-to-flip-to-previous-page-with-ndb-cursors – Lipis Jan 04 '15 at 18:39
  • Yes, those are in python. But in java it is not able to achieve. I think we can do it through index, but not sure. Still no one have not answered yet, so it is better to try myself through index – Angom Jan 11 '15 at 12:38

1 Answers1

1

If you are familiar with JPA you can give it a try. Have tested it and pagination works in GAE. I think they support JPA 1.0 as of now.

What I tried was, created an Employee entity. Created DAO layer and persisted few employee entities.

To have a paginated fetch, did this:

Query query = em.createQuery("select e from Employee e");
query.setFirstResult(0);
query.setMaxResults(2);
List<Employee> resultList = query.getResultList();

(In this example we get first page which has 2 entities. Argument to setFirstResult would be start index and argument to setMaxResult would be your page size)

You can easily change the arguments to query.setFirstResult and setMaxResults and have a pagination logic around it.

Hope this helps,

Regards,

jithin iyyani
  • 751
  • 1
  • 7
  • 19