如何将Java ENUM与<form:select> </form:select>一起使用

时间:2012-03-29 14:34:43

标签: java spring-mvc taglib

我正在尝试使用<form:select>标记在我的编辑内容.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

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

3 个答案:

答案 0 :(得分:6)

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

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

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

答案 1 :(得分:1)

您必须将枚举转换为集合并将其置于模型中。 然后在表单中使用它:像任何列表一样选择。示例代码:

控制器中的

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

在你的jsp中

Deploying Heroku Version 82d8ec66d98120ae24c89b88dc75e4d1c225461e
Traceback (most recent call last):
  File "<string>", line 1, in <module>
KeyError: 'source_blob'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
KeyError: 'source_blob'
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
Traceback (most recent call last):
  File "<string>", line 1, in <module>
KeyError: 'output_stream_url'
curl: try 'curl --help' or 'curl --manual' for more information

答案 2 :(得分:0)

相信如果您将Paperless.values()作为对象传递给您的jsp页面,然后取消引用名称和描述,您将获得所需的结果。

相关问题