枚举值为下拉列表

Bam*_*der 6 java enums jsp el spring-mvc

我正面临一个问题,从Enum类值填充下拉列表.我的枚举类代码是:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要与NEWYORK,FLORIDA和CALIFORNIA一起填写下拉列表.我正在创建并以这种方式将列表添加到Spring模型:

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

model.addAttribute("stateList", stateList);
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用以下方法填充JSP中的下拉列表:

<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>
Run Code Online (Sandbox Code Playgroud)

但是我得到一个异常"创建了异常:javax.el.PropertyNotFoundException:类'abc.xyz.constants.StateConstantsEnum'没有属性'fullState'."

我该如何解决这个问题?非常感谢

Joe*_*Joe 7

fullState是私人的,getState()是访问者.

<c:out value="${option.state}"></c:out>
Run Code Online (Sandbox Code Playgroud)

或者将你的吸气剂重命名为getFullstate().

  • 那么只需使用`$ {option}`.实际上,"fullstate"这个名字具有误导性.反过来说.它拥有州名缩写. (2认同)