• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing pagination in MVC - Servlets + JSP

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[MVC, Servlets + JSP, JPA, MySQL]
I am working on simple Blog application. I am using JPA to map entities to MySQL tables. Here is code excerpt from entities in question:
Entity Post:

Entity Comment:

Blog home page should contain summaries of 10 newest posts. So, in PostDAO object I have defined next method (returns all posts from db ordered by date):

I would like to implement pagination in some simple way, probably passing certain request parameters and reading data in jsp using jstl (i'm not yet familiar with jquery). Now, how to approach to implementing pagination in MVC? Which parameters I need to be attaching to request? How should I approach to implementing page navigation links (previous, page numbers, next) in JSP?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I'm paginating a set of results, I include the follow meta-information (along with any filtering params):

pageSize: the number of entries to show on a "page"
pageNumber: the current page number (I start with 1)
sortProperty: the property (column) on which to sort
descending: perform a descending sort if true, ascending otherwise

JPA makes it easy to obtain only the slice of data needed for the current page.
 
Vladimir Razov
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Bear Bibeault
Can you please explain in details what should be done in servlet? This is my first time implemeting pagination, so I am really struggling how to implement this concept.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The servlet is just a controller, it really shouldn't be doing anything but gathering the params and passing them on to classes in the model, which should handle all data gathering.

You use the pageSize and pageNumber values to determine the "slice" of data to be obtained. The setFirstResult() and setMaxResults() methods set limits for the query.

I use the sort parameters to create an order by clause for the query.
 
Vladimir Razov
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I have changed method in PostDAO object to use setFirstResult(pageNumber) and setMaxResult(postsPerPage):

Currently "getNewestPosts" named query is "SELECT p FROM Post p ORDER BY p.date DESC" and so it returns a list of Post objects. But, I would like, on my home page, to display next details: post_title, post_date,post_summary, number_of_post_comments. I know that I can use native query, instead of JPQL.

For example, I would like to use this query:

This query returns post data (specified) and number of post comments for every post.

Now, how should I approach to handling this type of data in servlet and in jsp? I don't understand how should I format this data in servlet and how to read it in JSP, now when the result is not a list of known-type objects (like it was with list of Post objects)
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like it was mentioned earlier, you should just return data from DAO to servlet and servlet can set request attributes and forward to JSP.
JSP should handle the presentation like formatting etc.

Once you have list of Post objects you can use JSTL forEach tag to display each item in the list.

Servlet Code:


JSP Code:

 
Vladimir Razov
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Maybe I didn't present situation properly)
Once again.
I am not getting list of Post objects as a result of executing query. There are some fields (concretely "number_of_post_comments") that is part of result. Or should I maybe create a map<key, value> where key would be post and value would be number_of_post_comments?
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you would need a JSON object or a Transfer Object (TO) to hold results.
Map would work too.
 
reply
    Bookmark Topic Watch Topic
  • New Topic