Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

10 enteries per page?

 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
I'm having difficult time in figuring out how do I go in displaying 10 entries per page in struts and having "next" link to take to another 10 enteries?
right now I am trying to use pager tag lib (with no luck)!

Any help is appreciated
-PC
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Sir,
This may help you
http://jsptags.com/tags/navigation/pager/pager-taglib-2.0.html#examples
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot pradeep...I mean it!
-PC
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
now I facing yet another problem. I'm using <logic:iterate> tag to fetech my records from database. If I were to use "for loop" atleast I can specify range i.e (for(i=0;i<=20;i++){display();} but in iterate how can I go on displaying only the first 10 records and then next 10 and so on...?
Can anybody help me here?
Thanks in advance
-PC
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to splilt your collection into sub-collections of size 10 each.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, what is wrong with the pager tag?
 
Junilu Lacar
Bartender
Posts: 7568
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Priyanka Chopda:
I'm using <logic:iterate> tag to fetech my records from database.

I don't know if I'm understanding you correctly but why are you doing that? Normally, you fetch your records at one go, put the results in a collection and then iterate through them; you don't use the iterate to retrieve records. As for controlling the start and length, see the offset and length attributes, respectively, of the iterate tag.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu ,Thanks for the info. I missed the offset attribute.
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu & Pradeep,
I was not aware of offset and length attributes.
I guess now it shouldn't cause any problems.
-PC
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Plz bear with me...
here is my code...now I'am getting one entry per page by using offset and length attributes og iterate tag; but how do I see rest of the enteries??
Can someone elaborate more on this problem?
Thanks a ton in advance

-PC
 
Junilu Lacar
Bartender
Posts: 7568
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want 10 per page, use length="10". Alternatively, you can put an Integer that holds the pageLength value in a context collection (either application, session, or request). E.g.
// in your Java code:
request.getSession().setAttribute("pageLength", new Integer(10));
// in your tag:
<logic:iterate ... length="pageLength" ...>
The offset is the index into the underlying collection that you want to start the iteration. That would normally be calculated as something like
pageLength * (currentPage - 1);
Where currentPage is 1-based (1st page = 1, 2nd page = 2, etc.)
So, if your underlying collection had 35 entries and you displayed 10 per page, offset for page 1 would be 0, offset for page 2 would be 10, and offset for page 3 would be 20.
As with length, you can put an Integer into a context collection and use the attribute key as the value for offset, too.
// in code:
if (nextPageRequested()) {
currentPage++;
} else if (previousPageRequested()) {
currentPage--;
} ...
request.setAttribute("pageStartIndex", new Integer(offset());
...
// in JSP:
<logic:iterate ... length="pageLength" offset="pageStartIndex" ... >
 
Priyanka Chopda
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bingo!!!its working fine now!
thanks junilu
 
bobby chaurasia
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priyanka,
Can you post your code as I have a requirement similar to yours except that instead of "next" or "previous" button I need to display a dynamic list of
page numbers eg :1 2 3 4 ( something like we have in java ranch forum) with each page to display 10 entries.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic