Mapping an Enum to form elements using struts2

3.2k views Asked by At

I'm trying to create a form which contains a radio button that maps to a class that extends java.lang.Enum.

<s:form action="corp_low_save">
  <s:textfield label="Corporate Client Name" name="name"/>
  <s:radio list="clientStatus.status" name="clientStatus.status"/>
  <s:textfield label="Primary Address Street" name="primaryAddress.street"/>  
  <s:submit />
</s:form>

The Action implements ModelDriven and is making a new instance of the following object available via getModel():

public class com...Customer extends java.lang.Object{
    protected java.lang.String name;
    protected com...ClientStatus clientStatus;
    protected com...Address primaryAddress;
    public com...Customer();
    public java.lang.String getName();
    public void setName(java.lang.String);
    public com...ClientStatus getClientStatus();
    public void setClientStatus(com...ClientStatus);
    public com...Address getPrimaryAddress();
    public void setPrimaryAddress(com...Address);
}

Here are the rest of the relevant classes. They were all generated by JAXB:

public class com...ClientStatus extends java.lang.Object{
    protected com...StatusString status;
    protected javax.xml.datatype.XMLGregorianCalendar since;
    protected java.lang.String business;
    public com...ClientStatus();
    public com...StatusString getStatus();
    public void setStatus(com...StatusString);
    public javax.xml.datatype.XMLGregorianCalendar getSince();
    public void setSince(javax.xml.datatype.XMLGregorianCalendar);
    public java.lang.String getBusiness();
    public void setBusiness(java.lang.String);
}

public final class com...StatusString extends java.lang.Enum{
    public static final com...StatusString NEW_CLIENT;
    public static final com...StatusString EXISTING_CLIENT;
    public static final com...StatusString RENEWAL;
    public static com...StatusString[] values();
    public static com...StatusString valueOf(java.lang.String);
    public java.lang.String value();
    public static com...StatusString fromValue(java.lang.String);
    static {};
}

I'm getting this error:

org.apache.jasper.JasperException: tag 'radio', field 'list', name 'clientStatus.status': The requested list key 'clientStatus.status' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name}

Any ideas about what the problem could be?

Edit: I think the issue is that ClientStatus is null. Since I simply instantiated a new Customer() its fields are null. This is a bummer because it requires me to encode duplicate information in two parts of my program. I can do something like this in the view:

  <s:radio name="clientStatus.status"
       list="#{'NEW_CLIENT':'New Client',
               'EXISTING_CLIENT':'Existing Client',
               'RENEWAL':'Renewal'}"/>

Or, in the controller, I have to explicitly instantiate the fields of Customer that my view needs. This also dashes my hopes of writing a general action that can process all sorts of JAXB objects by simply instantiating a given JAXB class and making it available via getModel().

Anyone have any ideas of how to recover from this unfortunate state of affairs?

Edit 2: Fixed the first form to what I would expect to work.

1

There are 1 answers

3
Steven Benitez On BEST ANSWER

Revised Answer

When the error message states:

could not be resolved as a collection/array/map/enumeration/iterator type

It means a java.util.Enumeration, not an enum.

With that said, you can still use an enum, you just need to pass an array or collection of the values you want available. For example, ClientStatus.values(), which returns an array.

Just add a getter to your action called getClientStatuses that returns the array (or a collection) of all of the values.

Original Answer

The list attribute needs to take all of the enum values. In your first example, it doesn't look like that is occurring. Instead, you are providing clientStatus. Isn't that an individual enum value?

In your revised example, you are giving the list attribute a map, so it is working.

I usually expose the enum values in my action. e.g., getClientStatuses.