使用 struts2 映射 Enum 以形成元素

发布于 2024-10-20 04:03:43 字数 2794 浏览 1 评论 0原文

我正在尝试创建一个表单,其中包含一个单选按钮,该按钮映射到扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2024-10-27 04:03:43

修订答案

当错误消息指出:

无法解析为集合/数组/映射/枚举/迭代器类型

这意味着java.util.Enumeration,而不是枚举。

话虽如此,您仍然可以使用枚举,只需传递您想要可用的值的数组或集合。例如,ClientStatus.values(),它返回一个数组。

只需向名为 getClientStatuses 的操作添加一个 getter,即可返回所有值的数组(或集合)。

原始答案

list 属性需要采用所有枚举值。在您的第一个示例中,看起来并没有发生这种情况。相反,您提供 clientStatus。这不是一个单独的枚举值吗?

在修改后的示例中,您为 list 属性提供了一个映射,因此它可以正常工作。

我通常在我的操作中公开枚举值。例如,getClientStatuses

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文