Java – use struts 2 to map enumerations to form elements

I'm trying to create a form that contains a radio button that maps to the extension Java Class of 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>

Action implements modelDriven and creates a new instance of the following objects through 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);
}

The following are other related courses They are 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 received this error:

Any ideas about what the problem might be?

Editor: I think the problem is that clientstatus is null Since I just instantiated a new customer (), its field is empty This is a rogue because it requires me to encode duplicate information in two parts of the program I can do 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 must explicitly instantiate the customer field required by my view This also destroys my desire to write a general operation that can handle various JAXB objects, just instantiate the given JAXB class and make it available through getmodel ()

Anyone has any idea how to recover from this unfortunate situation?

Edit 2: fixed the first table I wanted to work on

Solution

Revised answer

When the error message indicates:

It represents Java util. Enumeration, not enumeration

That said, you can still use enumeration. You just need to pass an array or a collection of values you want For example, clientstatus Values (), which returns an array

Just add a getter to the action named getclientstatuses, which returns an array (or collection) of all values

Original answer

The list attribute needs to get all enumeration values In your first example, it doesn't seem to happen Instead, you provide clientstatus Isn't this a separate enumeration value?

In the modified example, you provided a mapping for the list property, so it is running

I often expose enumeration values in actions For example, getclientstatuses

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>