如何在 <form:select> 中使用 Java ENUM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9927549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Java ENUM with <form:select>
提问by Webnet
I'm trying to use the following ENUM inside my edit content .jsp using the <form:select>
tag and can't find an example on how to do this.
我正在尝试使用<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;
}
}
....
The above content object is passed to my .jsp file as ${content}
.
上面的内容对象作为 .jsp 文件传递给我的 .jsp 文件${content}
。
I'm trying to do
我正在尝试做
<form:select path="content.Paperless">
<form:options items="${content.Paperless}" itemLabel="name"/>
</form:select>
It's throwing an exception....
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
它抛出异常....
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
I'm misunderstanding something about this but I can't pinpoint which one...
我对此有误解,但我无法确定是哪一个......
回答by Eugene Kuleshov
Your <form:select>
path is referring to a getter with name getContent() that returns an object that has getter getPaperless(). Perhaps you wanted to use just getPaperless() on your action model class.
您的<form:select>
路径指的是一个名为 getContent() 的 getter,它返回一个具有 getter getPaperless() 的对象。也许您只想在动作模型类上使用 getPaperless()。
Then to show list of enum values you just need to declare an empty options tag:
然后要显示枚举值列表,您只需要声明一个空的选项标签:
<form:select path="paperless">
<form:options/>
</form:select>
回答by Salim Hamidi
You have to convert enum to collection and put it to model. then use it in form:select like any list. Example code:
您必须将枚举转换为集合并将其放入模型。然后在 form:select 中使用它,就像任何列表一样。示例代码:
in your controller
在你的控制器中
model.addAttribute ("paperless", Arrays.asList(Paperless .values()));
in your jsp
在你的jsp中
<form:select ... items="${paperless}" itemValue="name" itemLabel="description"/>
回答by dardo
Believe if you pass Paperless.values() as an object to your jsp page, then dereference the name and description, you'll get the desired results.
相信如果您将 Paperless.values() 作为对象传递给您的 jsp 页面,然后取消引用名称和描述,您将获得所需的结果。