How to use enums with codes in Spring MVC?

Go To



public enum ArticlePublishStatus {
    DRAFT ("DRAFT"),

    private final String code;

    private ArticlePublishStatus(String code) {   

Screen Object (aka form backing object)

public class ArticleHeaderEditScreenObject extends EditScreenObject {
    private Integer articleId;
    private String title;
    private ArticlePublishStatus publishStatus;


<form:select path="publishStatus"
    itemValue="value" />


<select id="publishStatus" name="publishStatus">
    <option value="DRAFT" selected="selected">Draft</option>
    <option value="SUB">Submitted</option>
    <option value="PUB">Published</option>


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

java.lang.IllegalArgumentException: No enum constant com.siteadmin.domain.ArticlePublishStatus.PUB java.lang.Enum.valueOf(

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?

2012-04-04 01:21
by jacekn


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.

2012-04-04 02:57
by MarkOfHall


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.

See What is the reason for java.lang.IllegalArgumentException: No enum const class even though iterating through values() works just fine?

2016-06-15 00:04
by Sheng