6

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

BambooBlunder
  • 83
  • 1
  • 1
  • 4

2 Answers2

7

fullState is private, getState() is the accessor.

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

Or rename your getter to getFullstate().

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Joe
  • 80,724
  • 18
  • 127
  • 145
0

in your JSP you can use a like that :

<form:select path="*">
  <form:options items="${stateList}" itemLabel="fullState"  />
</form:select>

it will extract all element in your liste (stateList) and if you dont specify an itemLabel and itemValue, it'll take your enums values of course you have to set your getter to getFullState,and declare springmvc tags in your page

Slifer
  • 175
  • 1
  • 1
  • 10