Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help on Drop Down

 
Tim Cerillo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I hit the wall and can't figure out why I can't get the dropdown box to work.



I'm getting this error:


My TestForm has the getter/setter for testTypeName. I'm not sure what exactly it is. Do you I have to create another class for this? Thanks for your help.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If testTypeList is just a list or array of Strings, you're working too hard. Just do it like this:

You should only use the collection attribute when you have a collection or array of beans whose properties you want to use.

See the heading "options" in this link for more information on the htmlptions tag.
[ June 07, 2007: Message edited by: Merrill Higginson ]
 
Tim Cerillo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! It got me somewhere. However, since I'm using ArrayList, the object is not correctly being displayed. I'm assuming I need to convert the value into String. Do you have any suggestion on how to do it. I do not want to make the ArrayList an String array. Is there any other way? Thanks again.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand how your data is organized. Could you please post relevant portions of your ActionForm class, along with the code for any embedded objects in the ActionForm? That would help me answer your qestion.
 
Tim Cerillo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help!

Here is part of ActionForm:


To populate the list, I have a code in Action class that selects types from the database and put in ArrayList. After the execution, it looks something like


It looks like it is displaying the object itself since the contents of the ArrayList is object. I want to display the value of the object. Should I go back to the first solution?
[ June 07, 2007: Message edited by: Tim Cerillo ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the elements of the ArrayList are Strings, it should still work. Are you sure you're specfying <htmlptions> and not <htmlption>? If you were to specify <htmlption> that would cause this problem.
[ June 07, 2007: Message edited by: Merrill Higginson ]
 
Tim Cerillo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am specifying the correct tag. RAD tags it as invalid if I out option. It is really spitting out the object and not the value. Any workaround?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An ArrayList of Strings will undoubtedly work in this situation, as I've done it dozens of times. Is there a possibility that each element in the ArrayList is not a String, but some other object?

In any case, show us the code in your JSP. Then show us the output you're getting, as well as the contents of the list by doing a System.out.println() of the ArrayList.
 
Tim Cerillo
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just converted everything to ArrayList of String. It works fine. However, I want the actual value to be selected as default in the dropdown. How can I do this? Thaks again for all your help.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just set the value of the property in the form bean, and Struts will automatically show that value as the default. So, in the action class that forwards to this JSP, write something like:
 
richard b
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question in regards to this topic...

I ran into the same issue (with my dropdown displaying the object reference rather than the string value). I have it working another way, but I don't like the way it works.

My FORM BEAN:
public class JobForm extends ActionForm {
private List catDrop = null; // with getter/setter
private List locDrop = null; // with getter/setter

My ACTION CLASS:
Basically calls a service that returns an ArrayList of Strings to populate my bean.
e.g. jobForm.setLocDrop(service.getLocations)

My JSP:
<logic resent name="jobForm" property="locDrop">
<select name="location">
<option name="locationDropDown" value="All">All</option>
<logic:iterate id="locationDropDown" name="jobForm" property="locDrop">
<option value="<bean:write name="locationDropDown" property="loc"/>">
<bean:write name="locationDropDown" property="loc"/>
</option>
</logic:iterate>
</select>
</logic resent>

So... everything works as expected. The dropdowns both get populated with my list of locations/categories. When I submit my form, the values get sent to the action class, and I get my results fine. The problem is that I I want to persist the choices that were made in the drop downs, but they are repopulated and defaulted each time. Is there a better way to handle this so my drop down list choices are persisted? Hope I am making sense and gave enough info.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main advantage of using the <html:select> tag is that it will automatically show the previously selected value, whereas a plain <select> tag will not. In your case, the following should work:

[ September 15, 2007: Message edited by: Merrill Higginson ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic