Webnet Webnet - 21 days ago 8
Java Question

How to use Java ENUM with <form:select>

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.

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}
.

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


I'm misunderstanding something about this but I can't pinpoint which one...

Answer

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.

Then to show list of enum values you just need to declare an empty options tag:

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