使用 struts2 映射 Enum 以形成元素
我正在尝试创建一个表单,其中包含一个单选按钮,该按钮映射到扩展 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>
该 Action 实现 ModelDriven 并通过 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);
}
以下是其余相关类。它们都是由 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 {};
}
我收到此错误:
org.apache.jasper.JasperException:标签 '广播',字段'列表',名称 'clientStatus.status':请求的 列表键“clientStatus.status”可以 不被解决为 集合/数组/映射/枚举/迭代器 类型。示例:人或人。{name}
关于问题可能是什么的任何想法吗?
编辑: 我认为问题是 ClientStatus 为空。由于我只是实例化了一个新的 Customer(),它的字段为空。这很糟糕,因为它要求我在程序的两个部分中对重复信息进行编码。我可以在视图中执行类似的操作:
<s:radio name="clientStatus.status"
list="#{'NEW_CLIENT':'New Client',
'EXISTING_CLIENT':'Existing Client',
'RENEWAL':'Renewal'}"/>
或者,在控制器中,我必须显式实例化我的视图所需的 Customer 字段。这也破灭了我编写一个通用操作的希望,该操作可以通过简单地实例化给定的 JAXB 类并通过 getModel() 使其可用来处理各种 JAXB 对象。
有人知道如何从这种不幸的情况中恢复过来吗?
编辑2:将第一个表单修复为我期望的工作方式。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修订答案
当错误消息指出:
这意味着
java.util.Enumeration
,而不是枚举。话虽如此,您仍然可以使用枚举,只需传递您想要可用的值的数组或集合。例如,
ClientStatus.values()
,它返回一个数组。只需向名为
getClientStatuses
的操作添加一个 getter,即可返回所有值的数组(或集合)。原始答案
list
属性需要采用所有枚举值。在您的第一个示例中,看起来并没有发生这种情况。相反,您提供clientStatus
。这不是一个单独的枚举值吗?在修改后的示例中,您为
list
属性提供了一个映射,因此它可以正常工作。我通常在我的操作中公开枚举值。例如,
getClientStatuses
。Revised Answer
When the error message states:
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 providingclientStatus
. 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
.