枚举
public enum ArticlePublishStatus {
DRAFT ("DRAFT"),
SUBMITTED ("SUB"),
PUBLISHED ("PUB");
private final String code;
private ArticlePublishStatus(String code) {
this.code=code;
}
}
屏幕对象(又名表单支持对象)
public class ArticleHeaderEditScreenObject extends EditScreenObject {
private Integer articleId;
private String title;
private ArticlePublishStatus publishStatus;
看法
<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>
例外
草稿工作正常,因为相应枚举的名称和代码是相同的。保存已提交或已发布的页面失败。
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)
我看到它在根据其代码选择枚举时遇到问题,但我仍然不知道该怎么做。有很多关于枚举的主题,但没有找到任何可以帮助这个主题的东西。是否有一种方法需要实现枚举来解决它?