【问题标题】:How to use Java ENUM with <form:select>如何将 Java ENUM 与 <form:select> 一起使用
【发布时间】:2012-03-29 14:34:42
【问题描述】:

我正在尝试使用 &lt;form:select&gt; 标记在我的编辑内容 .jsp 中使用以下 ENUM,但找不到有关如何执行此操作的示例。

public class Content implements Serializable {
    public enum Paperless { 
        NONE(null, ""),
        EDELIVERY_RECOMMENDED("EDELIVERY_RECOMMENDED", "Recommend eDelivery"),
        EDELIVERY_REQUIRED("EDELIVERY_REQUIRED", "Require eDelivery"),
        EDELIVERY_REQUIRED_JUSTIFICATION("EDELIVERY_REQUIRED_JUSTIFICATION", "Require eDelivery w/out justification");

        private String name;
        private String description;
        Paperless(String name, String description) {
            this.name = name;
            this.description = description;
        }
        public String getName() {
            return this.name;
        }
        public String getDescription() {
            return this.description;
        }
    }
....

上面的内容对象作为${content}传递给我的.jsp文件。

我正在努力

<form:select path="content.Paperless">
    <form:options items="${content.Paperless}" itemLabel="name"/>
</form:select>

它正在抛出异常...... org.springframework.beans.NotReadablePropertyException: Invalid property 'content' of bean class [com.fettergroup.cmt.model.Content]: Bean property 'content' is not readable or has an invalid getter method: Does the return type of the getter match the parameter

我对此有一些误解,但我无法确定是哪一个......

【问题讨论】:

  • Content 中的 List&lt;Paperless&gt; getPaperlessOptions() 方法怎么样?这将要求您在页面中写 items="${content.paperlessOptions}"。

标签: java spring-mvc taglib


【解决方案1】:

您的&lt;form:select&gt; 路径指的是一个名称为getContent() 的getter,它返回一个具有getter getPaperless() 的对象。也许您只想在您的动作模型类上使用 getPaperless()。

然后要显示枚举值列表,您只需要声明一个空选项标签:

<form:select path="paperless">
   <form:options/>
</form:select>

【讨论】:

  • 不幸的是,这不起作用:-\我得到与上述相同的错误
  • 我明白了。你能发布你的 声明吗?
  • &lt;form:form commandName="content" method="POST" action="save.html"&gt;
  • 我就是这么想的。请在我的答案中查看正确的
【解决方案2】:

您必须将枚举转换为集合并将其放入模型。 然后在 form:select 中使用它,就像任何列表一样。示例代码:

在你的控制器中

 model.addAttribute ("paperless", Arrays.asList(Paperless .values()));

在你的jsp中

<form:select ... items="${paperless}" itemValue="name" itemLabel="description"/>

【讨论】:

    【解决方案3】:

    相信如果你将 Paperless.values() 作为一个对象传递给你的 jsp 页面,然后取消引用名称和描述,你会得到想要的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 2020-04-17
      • 1970-01-01
      • 2015-07-21
      相关资源
      最近更新 更多