You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Adam Murray <ad...@fortifysoftware.com> on 2004/08/17 01:41:40 UTC

multiselect with a map-backed form property

I have a form with a dynamically generated list of constraints that can
be turned on or off, and each constraint has a set of values associated
with it. My form has two relevant fields:

private String[] selectedConstraints;
private Map values;

public String[] getSelectedConstraints() {
    return selectedConstraints;
}
public void setSelectedConstraints(String[] selectedConstraints) {
    this.selectedConstraints = selectedConstraints;
}

public Object getValues(String key) {
    return values.get(key);
}
public void setValues(String key, Object value) {
    values.put(key, value);
}


To keep track of which values are associated with which constraint, I
use the string from the selectedConstraints array as the key into the
values map. My jsp page looks like this:

<c:forEach var="constraint" items="${constraints}">

  <tr><td>

    <html:multibox property="selectedConstraints">
      <c:out value="${constraint.propertyID}"/>
    </html:multibox>
    <c:out value="${constraint.propertyName}"/>

  </td><td>

     <html-el:select property="values(${constraint.propertyID})">
       <html:optionsCollection name="constraint"
property="allowableValues"/>
     </html-el:select>
...


The above works perfectly fine as long as only want to select a single
item from the select drop-down box. The problem is some of the
constraints can have multiple values, so I changed the select to a
multi-select:

     <html-el:select multiple="true"
property="values(${constraint.propertyID})">

But then the form is only ever populated with the *first* string
selected from the multiselect box. Since a map can hold a String[] as
easily as a String, I'm a little confused by this behavior. I tried the
following things with no luck (these changes are in the struts action
that forwards to the jsp above):

1) inserting an empty String array into the values map for each key in
an attempt to coerce the user of String arrays

2) inserting a wrapper class into the map for each key:
public class SelectedValues {
    private String[] selectedValues;
    public String[] getSelectedValues() {           
        return selectedValues;
    }
    public void setSelectedValues(String[] selectedValues) {
        this.selectedValues = selectedValues;
    }
}

and I updated my jsp to:
     <html-el:select multiple="true"
property="values(${constraint.propertyID}).selectedValues">

but that causes the following exception:
javax.servlet.ServletException: BeanUtils.populate
	org.apache.struts.util.RequestUtils.populate(RequestUtils.java:1254)
...
root cause:
java.lang.IllegalArgumentException: No bean specified
	org.apache.commons.beanutils.PropertyUtils.getPropertyDescriptor(PropertyUtils.java:837)
...


I can get the multiselect to work if I create a String array for each
constraint, but that defeats the purpose since this is supposed to be
dynamically generated. I'm out of ideas for other things to try with the
map-backed approach. Can someone tell me what I'm doing wrong here?

Thanks,
Adam

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org