作为下拉列表的枚举值
我面临着从枚举类值填充下拉列表的问题。我的枚举类代码是:
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;
}
}
我想用纽约、佛罗里达和加利福尼亚填充下拉列表。我正在以这种方式创建列表并将其添加到 Spring 模型中:
List<StateConstantsEnum> stateList = new ArrayList<StateConstantsEnum>( Arrays.asList(StateConstantsEnum.values() ));
model.addAttribute("stateList", stateList);
然后我尝试使用以下方法填充 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>
但我收到一个异常 “已创建异常:javax.el.PropertyNotFoundException: 类“abc.xyz.constants.StateConstantsEnum”没有属性“fullState”。“
我该如何解决这个问题?非常感谢帮助
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
fullState
是私有的,getState()
是访问器。或者将您的 getter 重命名为
getFullstate()
。fullState
is private,getState()
is the accessor.Or rename your getter to
getFullstate()
.在 JSP 中,您可以使用类似的方法:
它将提取 liste (stateList) 中的所有元素,如果您没有指定 itemLabel 和 itemValue,它将采用您的枚举值
当然你必须将 getter 设置为 getFullState,并在页面中声明 springmvc 标签
in your JSP you can use a like that :
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