• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tags for ordered iteration

 
Jeremy Davis
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody know if there is a tag struts or otherwise that allows ordered iteration of a collection? For instance, iterate over a collection of orders based on dollar amount.
Thanks in advance,
Jeremy
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I take it that the collection is unordered as sent to the JSP page?
I'd take an approach where the servlet controller for the page took care of the ordering, then use any of the existing iteration tags (JSTL c:forEach, for example) and not worry about processing the order on the page.
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeremy,
I used similar method suggested by Mr. Bibeault.
I adopted the TransferObject pattern, where each record retrieved inside the database, and used the ORDER BY clause.
Thus, the data selected from the DB are in the right order.
After that, I used TransferObject to store the data obtained from the DB, and use a Vector to store the TransferObject. And thus, the objects inside the Vector are in the right order as well.
And finally, when I want to display it out to the JSP, I simply use a while loop, with the size() of the Vector, and I can "print" the data in order.
Does this help solving your problem?
Nick.
 
Jeremy Davis
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick replies!
I have a collection of order objects displayed as tabular data, and I want the user to be able to filter the data by clicking on the top (header) column which represents properties of the order object. For instance, a user could filter this month's orders by ship date, dollar amount, etc.
I don't want to round trip to the database. My first guess was to write a couple of methods for the object holding the collection to filter/order the collection and leave the page alone -- it would take care of itself after the action forwards to it.
But I was wondering if there was already a tag that would allowing such an iteration.
Thanks again,
Jeremy
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about re-ordering on the server or client side? For a tag to be re-rendered you need to make a round-trip to the server (where it's easy to either go back to the DB or resort a cached collection).
Or are you looking for a tag set that will emit a bunch of Javascript to manipulate everything on the client?
 
Jeremy Davis
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,
Sorting on the server is fine. I just don't want to have to go back to the db since it takes a while to build this report.
I want something like ESPN's statistics pages where the categories are sortable: ESPN NHL Stats
The object I'm referencing is persisted so I think it would be useful to be able to sort the display from the page.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ESPN site is definitely going back to the server for the re-ordered data. If you don't want to go back to the DB, you can resort the cached data.
 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeremy Davis,
Its fine that you dont want to make a database hit, for that you need to implement caching, i.e. store data temporarily in some collection at server. Implementing this is not very easy.
My first guess was to write a couple of methods for the object holding the collection to filter/order the collection and leave the page alone -- it would take care of itself after the action forwards to it.
I am sure that implementing caching will be more difficult. Say User "A" views set of data, while at the same time user "B" updates the database. Now user "A" filters the data by clicking on the top (header) column, and your application will show him old data, causing inconsistency.

But I was wondering if there was already a tag that would allowing such an iteration.
In MVC architecture (which struts uses) most of the computaion is done in servlet / Action Class. JSPs are used only for presentation. So i will recommend you to consider Bear Bibeault's suggestion.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!