views:

37

answers:

2
State:
<html:select property="product.stateId" size="1" onchange="loadProducts();loadProducts2();">
    <html:options collection="stateList" property="id" labelProperty="name"/>
</html:select>

I want to add another option to the list of states such as, "Please select state," in the drop down menu. I think I need to modify the Java collection object..

+1  A: 

Just add a <html:option> tag which represents that. The TLDDOC allows that:

This tag can be used multiple times within a single <html:select> element, either in conjunction with or instead of one or more <html:option> or <html:optionsCollection> elements.

BalusC
Do you put an input tag inside option?
atomical
Huh? How is this comment related to this question/answer? In the future just ask a new question for every new and independent question. At any way, this is not possible as per the HTML specification. If you want a *combobox* (an editable dropdown), you'll have to look for a JavaScript flavored solution. Struts doesn't have such a component out of the box. You can find [here](http://www.google.com/search?q=jquery+combobox) several examples based on [jQuery](http://jquery.com) JS library.
BalusC
A: 

In my opinion, I would rather set that "Please select state" directly into your stateList in your model before you come to the JSP page. Basically, after you populate your stateList, you will add this statement something like this:-

stateList.add(0, new IdName("-1", "Please select state"));

This way...

  1. Your Struts/JSP code can remain the way it is.
  2. Less clutter in your JSP code.
  3. You can ensure if the form validation fails, Struts will correctly prefill the right value back.
limc
It's only more clutter in the model :) This is IMO really the responsibility of the view, not the model. As to prefilling, I have no hands on experience with Struts, but its competitor, JSF, will do it correctly.
BalusC