Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help with pagination in JSP?

 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an ArrayList contain data return from database. The list already sorted. I want to walk thru this list and display 10 record per page with NEXT , PREVIOUS link.

the code in using JSP embedded with JAVA scriplet <% %>, so there is no special taglib.

I have search thru the posts but could not find good sample code. Can any one please help with this. Thank in advance


Many thanks in advance.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65228
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would be best to let the DB do the sorting and paging. Please read the JSP FAQ entry on this topic.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read the FAQ and i don't think it is the efficient approach for my case, since my data is collecting from different source, and in the end i stuff into the one Object as ArrrayList.

so any sample code suggestion , thank in advance
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jay lai:
since my data is collecting from different source,


Can you explain, a little further, what you mean by 'different source'?
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically is not coming from the same query, and also for now I don't want to touch the query b/c somebody else may invoke it.
So on the jsp now I got the whole Array List of data sorted, I just need some sample code that do the paging using scriplet in JSP.

Thnks in advance
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSTL's forEach tag has begin and end attributes.
In your form or hyperlinks, send the desired items from the list.

You will either need to store the whole list in session which could result in a lot of memory consumption if the results are large or, read the entire result from the database with each call and ignore all but the rows that you want to show. This can also be inefficient if the database is returning large result sets.
 
Pallavi Srivastava
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is the code for pagination.It will display number of records ,previous-next link,page number and can also customise number of records displayed on screen.



You have to customise according to your requirement.
Hope this helps.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65228
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good lord, what a mess.

Regardless of how you are obtaining the data, the paging should not be happening in the UI layer. Rather your model should have an API that allows the UI to ask for the records for a particular page, which should return only the relevant records.

This not only keeps your UI code "cleaner", but also allows you to change the details at any later date on how the page of data is obtained.

Doing this sort of thing in the UI is a poor practice and should be aoivded.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Pallavi Srivastava;
I think that what I am looking for and try to customize it.
Thanks again.
 
Pallavi Srivastava
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bear for the tip.I am able to develop logic of the application but still weak at architect level.As you said, I will make a pagination component and remove the mess from front end.

Thanks again.
 
jay lai
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's is a good suggestion from Bear. But sometimes, the application is not in the new redesign mode, sometimes you need to modified the OLD code therefore, there is nothing much in design or architech that you can change in the mean time while time is in constrant.

thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65228
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, how many times have I heard management say that? And while it may appear to be a logical conclusion, frequently it is not. Sometimes you'll spend a far greater amount of time trying to get a poorly constructed system to work the way you want it to than it would take to refactor the whole thing.

Management tends to perfer the former though because, even if it is buggy, they can see something, which gives them the illusion of progress. Whereas a refactor gives them the feeling that nothing is happening.

But this probably belongs in the process forum...
[ June 08, 2007: Message edited by: Bear Bibeault ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic