Mapping Enum to form elements using struts2

I am trying to create a form containing a switch 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 creates a new instance of the following object, accessible 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 created 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 get this error:

org.apache.jasper.JasperException: tag 'radio', field 'list', name 'clientStatus.status': requested list key 'clientStatus.status' may not allow collection / array / map / enumeration / iterator type. Example: people or people. {Name}

Any ideas on what could be the issue?

Edit: I think the problem is that ClientStatus is null. Since I was just creating an instance of the 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 a view:

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

Or, in the controller, I must explicitly create instances of the Client fields that I need. It also robs my hope of writing a general action that can handle all kinds of JAXB objects by simply creating an instance of this JAXB class and making it accessible through getModel ().

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

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

+4
source share
1 answer

Revised Answer

If the error message says:

cannot be resolved as a set / array / map / enumeration / iterator type

This means java.util.Enumeration , not an enumeration.

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

Just add a getter to your action called getClientStatuses , which returns an array (or collection) of all values.

Original answer

The list attribute must take all enumeration values. In your first example, it does not look like this is happening. Instead, you provide clientStatus . Isn't that a separate enumeration value?

In your updated example, you specify the map attribute to the list attribute, so it works.

I usually expose enumeration values ​​in my action. e.g. getClientStatuses .

+1
source

Source: https://habr.com/ru/post/1342050/


All Articles