This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes Paging in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Paging in Struts" Watch "Paging in Struts" New topic

Paging in Struts

Luv Bhatia

Joined: Apr 01, 2004
Posts: 16
How can i paging into struts. I have a page where i want to display 20 records(out of >1200) at a time and then browse through one page to another.Can anyone publish some code to achieve this. I have already tried but it is taking me no where.
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
Pagination is not a part of struts mean it wont provide any utility for this .u need to implement this in whatever format u want .
There are many ways of implementing this pagination (but first decide up on the way in which u need pagination ie what are the things u r displaying like next,first,last,previous,total recorsd and all .......) then i will have 2-3 ways of implementing this
1. First store all the Results in session(If u feel that going to DB everytime is costly ) and then write a servlet(Action class which has a form with attributes like startRecord,EndRecord,totalRecords,pageactionString(like previous,next..))
and everytime store only that page records in an arraylist and iterate this in ur jsp .
(If u want sample code for this i can help u out )
2. There is some third party tool called iBATIS which provides ab api for this pagination .However this internally uses the same logic as in 1sr idea.
com.ibatis.common.util.PaginatedList is the one which will help u .This is a part of Ibatis jar .u can get help from regarding this
3.There is a website called which provides a custom tag for pagination .This is the most simplest if u can succed in the confifurations given by them in thier website while using the TLD/Tag
U go through all these and come out which is comfortable .i Feel 1st idea is easy if u can spare sometime in writing the logic
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1297
u can use a pageable Iterator or using javascript and jsp to page a iterator

BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
Oracle Certified Master Java EE 5 Enterprise Architect
Srikanth Shenoy
Ranch Hand

Joined: Jan 24, 2004
Posts: 184
Paging... Ahh one of my favorites.
While one of the solutions suggested earlier (Storing in Session) works for small result set paging but fails (in terms of scalability) for large number of rows (like you have - 1000+ rows).
Below I have posted a solution that compromises neither performance nor scalability to achieve this. This is an extract of the complete solution presented in my book in Chapter 6 - "JSPTags integration with Struts to achieve scalable scrolling functionality with very large resultsets".
Here are my suggestions based on my experiences for dealing Prev, Next functionality in JSPs with large scrollable result sets.
There are two aspects to the Prev/Next functionality.
The first is the display and the second is database/memory data retrieval
I have found the following two third party (Both are free and one is open source) very helpful. They are:
1) Pager Taglib from
2) Display Tag from sourceforge (
Both of them are very good, very powerful and they can pretty much take care of paging, display, formatting issues.
Coming to the second point - Data retrieval. A plain vanilla ValueListHandler that retrieves the data from the session does not cut it.
By plain vanilla I mean "All the data is fetched at one shot and is iterated over at increments of 10 (say)"
This approach works well if the number of rows is probably less than 100
When the row count goes well above 100 certain problems appear. Let us say 1000 rows were fetched.
1)The data has to be stored in HTTP Session. If each row takes up 500 bytes, you have a HTTP Session of size 500K. Multiply by that by number of users and you have run out of space....
2)If a shared cache is used, then the size of the cache is reduced, but what happens if the data in cache is removed (based on LRU - Least recently used).
3)Keeping the database connection alive is not a solution either.
Your solution has to address such questions. I have found it better to introduce a bit of database dependency in my solution (Standards freaks might frown on my solution, but sometimes rules have to be broken for getting even a decent performance)
My solution is a hybrid of fetching database rows on demand and fetching all rows. All databases provide features to uniquely identify the current row.
In Oracle it is the ROW_NUM
In DB2 it is the ROW_NEXT
When the first page is requested, fetch the data and track the ROW_NUM (or ROW_NEXT). A user who has requested the first page, will most certainly fetch more. Hence do a eager pre-fetch for the next three pages (say) after the first page is served (maybe asynchronously on a low priority thread - so your CPU has time for other higher priority tasks). This solution WORKS and works well.
You can abstract the above implementation behind a spicy ValueListHandler and ValueListIterator (Core J2EE Patterns).
(Sometimes plain-vanilla just doesnt cut it )

Now onto the Standards turf - Think about it - How many times in the lifetime of an application do you really port the database to a different vendor?
Even if you port it, the portability we have sacrificed in the above solution is neglible and is trivial to change when migrating to a different database.
Hope this helps.
[ April 28, 2004: Message edited by: Srikanth Shenoy ]

Srikanth Shenoy
Author of Struts Survival Guide : Basics to Best Practices
Srikanth Shenoy
Ranch Hand

Joined: Jan 24, 2004
Posts: 184
Oh.. I forgot... The above solution I presented above was initially suggested by me in the two JavaRanch threads from JSP forum.
You will find some more interesting stuff there.
Sean Walker
Ranch Hand

Joined: Feb 04, 2004
Posts: 64
Often when you want to expose paging through a list for users you must allow sorting by user selected field. I don't believe your method will work even if this field is fixed because the ROW_NUM sequence isn't generated per query, but is instead fixed for the table.
Correct me if I am wrong.
[ April 30, 2004: Message edited by: Sean Walker ]

Sean Walker<br />PMP, SCEA, SCWCD, SCJP
Luv Bhatia

Joined: Apr 01, 2004
Posts: 16
Thanx all of you, especially Srikanth Shenoy i am working on ur tips.
s. anitha

Joined: Jan 18, 2005
Posts: 18
Thank u for ur Timely and great help, Shrikanth Shenoy

Its Working for me

Ravi Seshu
Ranch Hand

Joined: Mar 19, 2005
Posts: 38
hi, i m intersted with ur conversation...but i have a small problem...may be u can suggest me...,

i m facing probs with the struts in jps for add a record into a db using ejb in jboss server (liferay portals)...wat i want is i have placed all my set and get methods in forms and action classes check for submit button name...

i dont find any information it went into set method of my submit method...

<html:submit property="policyAdd"></html:submit>

setpolicyAdd() getPolicyAdd()...

and in action clas si m checking the name using "add Policy".equals(form.getPolicyAdd())...
also i set up a server trace for in each of the submit get set and action forward...but nothing is going inot it...

can u suggest...any....
thz adv
I agree. Here's the link:
subject: Paging in Struts
Similar Threads
paging through struts
Looking for documentation on using displaytag with struts 2
how i use paging in struts.
How to display 10 records per page
need help in paging....