Cool.. I didn't even know there was a BeanComparator.. Never needed it I
guess.
As for the sorting though it might be easier to use displayTag
(http://displaytag.sourceforge.net/)
I havent used it yet myself but I hear great things about it.
As for the searching.. If you have an EJB in the backend you can create a
finder by firstname/lastname/fullname or whatever you want to search on.
Then just have your action call the appropriate EJB finder based on the
fields provided to search on.
You don't really need a hashtable for it that way.
If you want to go the hashtable approach (if you cant use Ejbs or some other
reason) note that you will have to provide a hashtable keyed by every value
you want to search on.. That could become quite resource intensive and not
really recommended.
Instead just have a simple method.
//might be better as a bean property/value array implemented with
reflections/beanutils if you don't know how many fields will need to be
searchable
private MyResultBean search(String firstname. String lastname, Integer age)
{
MyResultBean resultBean = null;
if( firstname != null )
{
//search collection via firstname
}
if( lastname!= null )
{
//search collection via lastname
}
if( age != null )
{
//search collection via age
}
return resultBean;
}
Obviously there are many different possible approaches as other people on
this list might suggest.
But hopefully this gives you some direction.
-Tim
-----Original Message-----
From: Jason Lea [mailto:[EMAIL PROTECTED]
Sent: Thursday, October 23, 2003 11:04 PM
To: Struts Users Mailing List
Subject: Re: sorting and searching in Struts/tiles
I have included a quick example of doing this by using a list of
hashmaps. The hashmap holds the text that may appear in the pulldown.
list = new ArrayList();
for (Iterator i = myObjects.iterator(); i.hasNext();) {
Map map=new HashMap();
map.put("firstName","Joe");
map.put("lastName","Smith");
map.put("fullName","Joe Smith");
map.put("someOtherField","blue");
list.add(map);
}
to sort the list we use can do this:
Collections.sort(list, new BeanComparator("lastName"));
(this uses the BeanComparator from here
http://jakarta.apache.org/commons/beanutils.html)
This will sort ascending by default, so to get decending I can do this
afterwards...
Collections.reverse(list);
Dinh Nguyen wrote:
>Hi Tim,
>
>In this case, I have to display the sorted list (ascending and
>descending) for names, etc. How do I do this? How can I pull the
>objects from the back-end.
>I am thinking to use collections.sort(list, comparator)as you
>mentioned or use sortedset and using arraylist (or hashset) to hold
>objects.
>For searching, I am thinking to use hashtable, but have no idea
>what's the next step.
>I use the EJB as back-end and has not implemented it yet.
>
>If you can give me feedback on this or give me some kinds of ideas,
>then it'll be great.
>Thanks for your help.
>Dinh Nguyen
>
>--- In [EMAIL PROTECTED], "Chen, Gin" <[EMAIL PROTECTED]> wrote:
>
>
>>I'm not sure I understand this question but wouldn't you just
>>
>>
>implement some
>
>
>>comparators?
>>Then based on the sort option selected you can call something like:
>>Collections.sort(list, comparator)?
>>As for the search just have a java module to search thru the
>>
>>
>collection.
>
>
>>I'm not quite sure how what this question really has to do with
>>
>>
>Struts.
>
>
>>Unless you use something like the displaytags to display a
>>
>>
>sortable list for
>
>
>>the names.
>>-Tim
>>
>>-----Original Message-----
>>From: Ruth, Brice [mailto:[EMAIL PROTECTED]
>>Sent: Thursday, October 23, 2003 1:26 PM
>>To: Struts Users Mailing List
>>Subject: sorting and searching in Struts/tiles
>>
>>
>>[Forwarded from Dinh Nguyen, his emails aren't reaching the list]
>>
>>Dinh Nguyen wrote:
>>
>>Hi,
>>
>>I am wondering how do you do the sorting and searching in struts.
>>Let say, when a person views a list of collections (for example,
>>
>>
>the
>
>
>>list consists of 5 people, each person has first name, last name,
>>age), there is a drop-down menu on the bottom of the table/page
>>
>>
>says
>
>
>>that:
>>1) Sort has two options: a) Ascending b) Descending
>>2) Search has three options a) Last Name b) First Name c) Age
>>
>>Since data is not stored in database yet.
>>so in this case how can I do it using Struts?
>>
>>Thanks for your help.
>>
>>DN
>>
>>
>>-------------------------------------------------------------------
>>
>>
>--
>
>
>>To unsubscribe, e-mail: [EMAIL PROTECTED]
>>For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>-------------------------------------------------------------------
>>
>>
>--
>
>
>>To unsubscribe, e-mail: [EMAIL PROTECTED]
>>For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>
>
--
Jason Lea
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]