Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum values as dropdown list

I am facing an issue populating a dropdown list from Enum class values. My enum class code is:

package abc.xyz.constants;

public enum StateConstantsEnum
{
           NEWYORK("NY"), 
            FLORIDA("FL"), 
            CALIFORNIA("CA"), 

    private String fullState;

    private StateConstantsEnum( String s )
    {
        fullState = s;
    }

    public String getState()
    {
        return fullState;
    }
}

I want populate dropdown list with NEWYORK, FLORIDA and CALIFORNIA. I am creating and adding the list to Spring model this way:

List<StateConstantsEnum> stateList = new ArrayList<StateConstantsEnum>( Arrays.asList(StateConstantsEnum.values() ));

model.addAttribute("stateList", stateList);

Then I am trying to populate the dropdown in JSP using:

<select name="${status.expression}" name="stateLst" id="stateLst">
    <option value=""></option>
        <c:forEach items="${stateList}" var="option">
                <option value="${option}">
                    <c:out value="${option.fullState}"></c:out>
                </option>
        </c:forEach>
</select>

But I am getting an exception "Exception created : javax.el.PropertyNotFoundException: The class 'abc.xyz.constants.StateConstantsEnum' does not have the property 'fullState'."

How do I fix this problem? Help much appreciated

like image 310
BambooBlunder Avatar asked Sep 29 '11 02:09

BambooBlunder


1 Answers

fullState is private, getState() is the accessor.

<c:out value="${option.state}"></c:out>

Or rename your getter to getFullstate().

like image 156
Joe Avatar answered Sep 30 '22 13:09

Joe