enum
public enum ArticlePublishStatus {
DRAFT ("DRAFT"),
SUBMITTED ("SUB"),
PUBLISHED ("PUB");
private final String code;
private ArticlePublishStatus(String code) {
this.code=code;
}
}
Screen Object (aka form backing object)
public class ArticleHeaderEditScreenObject extends EditScreenObject {
private Integer articleId;
private String title;
private ArticlePublishStatus publishStatus;
View
<form:select path="publishStatus"
items="${screenObject.getArticlePublishStatusOptionList()}"
itemLabel="label"
itemValue="value" />
html
<select id="publishStatus" name="publishStatus">
<option value="DRAFT" selected="selected">Draft</option>
<option value="SUB">Submitted</option>
<option value="PUB">Published</option>
</select>
Exception
Draft works fine, as the name and code of corresponding enum are the same. Saving the page with Submitted or Published fails.
org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type com.siteadmin.domain.ArticlePublishStatus for value 'PUB'; nested exception is java.lang.IllegalArgumentException: No enum constant com.siteadmin.domain.ArticlePublishStatus.PUB
org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
java.lang.IllegalArgumentException: No enum constant com.siteadmin.domain.ArticlePublishStatus.PUB
java.lang.Enum.valueOf(Enum.java:236)
I see that it's having problems selecting enum based on its code but I still have not figured out what to do about it. There's lots on the subject of enums but haven't found anything that would help this one. Is there a method that enum needs implemented to address it?
With out seeing the implementation of getArticlePublishStatusOptionList()
, this a bit of a guess...
If you used the toString
value of ArticlePublishStatus
as the value part of the Option returned from getArticlePublishStatusOptionList()
, then the Spring MVC Enum Converter could bind your enum using .valueOf
.
You should use
<option value="SUB">Submitted</option>
<option value="PUB">Published</option>
Spring use .valueOf("SUBMITTED"); to convert the value to enum. It has nothing to do with the enum internal value.