View Full Version : Sorting inside the Controller or the view?
TSH
Aug 14th, 2007, 05:02 AM
Hello,
as mentioned in this thread (http://forum.springframework.org/showthread.php?t=42595) I have a itemController to create a bean called itemList.
This list shall include dvds, books, newspaper-articels or several other items/beans.
I want to have several views on this list, e.g. an alphabetically sorted view or a chronologically sorted view. How do I sort those beans?
1) Sorting inside the controllers:
I could change the itemController to itemControllerAlphabetically and itemControllerChronologically and implement different ways to create the bean list.
2) Sorting inside the view
I could keep my itemController and use jsp to sort the beans.
Which one would you prefer?
tanmay
Aug 14th, 2007, 07:48 AM
1) Sorting inside the controllers:
I could change the itemController to itemControllerAlphabetically and itemControllerChronologically and implement different ways to create the bean list.
I would not suggest to have different controller just for sorting. I will suggest you send the sorting order(ASC/DESC) to your itemController and get sort result from database on that basis.
2) Sorting inside the view
I could keep my itemController and use jsp to sort the beans.
I will suggest get the sorted data from database, that will be faster.
Colin Yates
Aug 14th, 2007, 11:07 AM
Hello,
as mentioned in this thread (http://forum.springframework.org/showthread.php?t=42595) I have a itemController to create a bean called itemList.
This list shall include dvds, books, newspaper-articels or several other items/beans.
I want to have several views on this list, e.g. an alphabetically sorted view or a chronologically sorted view. How do I sort those beans?
1) Sorting inside the controllers:
I could change the itemController to itemControllerAlphabetically and itemControllerChronologically and implement different ways to create the bean list.
2) Sorting inside the view
I could keep my itemController and use jsp to sort the beans.
Which one would you prefer?
I am not sure there is a choice actually....it has to come from the DB simply because of paging. If you are not using paging, then either DB or Controller (but I would still suggest DB ;)) but if you are paging, there is no other option.
TSH
Aug 14th, 2007, 03:05 PM
What about a abstract bean called "BeanSorter" with its children "BeanSorterAlphabetic" and "BeanSorterChronological". Then each itemController can have its appropriate sorter. ??
JEisen
Aug 14th, 2007, 03:31 PM
Like other people have said, I'd do this through the DB. Don't make more objects in Java for what Java doesn't even need to see.
Doing it through a single controller also lets you handle it in Ajax later, if you wish, resorting tables on the fly. ;)
TSH
Aug 14th, 2007, 06:07 PM
Unfortunately I don't get the items entirely from a DB. I have to collect the data (label, date, description,...) from different sources. I can't sort them before my beanList is created.
nllarson
Aug 14th, 2007, 08:55 PM
Use displayTag in your jsp. Sorting and paging work out of the box. You just drop your list in, it will do the rest.
http://displaytag.sourceforge.net/11/
TSH
Aug 15th, 2007, 10:18 AM
That's a great library! Thank you. I'm using it right now.
blueskies
Aug 15th, 2007, 12:19 PM
I think you should have a separate function or bean for sorting , not separate controllers. Your controller could take some arguments which indicate what/how to sort.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.