如何在JSP中显示枚举结构的值?我使用Spring MVC来实现我的项目。
非常感谢!
public enum ProjectStatusEnum {
INITIAL(0,"Initial"),ONGOING(1,"Ongoing"),CLOSED(2,"Closed");
private int value;
private String key;
ProjectStatusEnum(int value , String key){
this.value=value;
this.key = key;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
答案 0 :(得分:4)
在请求的属性中添加枚举值:
// ProjectStatusEnum.values() return an array of ProjectStatusEnum
request.setAttribute("enum", ProjectStatusEnum.values());
最后,在你的JSP中:
<ul class="dropdownmenu">
<c:forEach items="${enum}" var="entry">
<li>${entry.key} (${entry.value})</li> <!-- for example -->
</c:forEach>
</ul>
答案 1 :(得分:0)
在ModelAttribute类中定义要绑定到ENUM类类型的变量
public class YourCommand implements Serializable{ private ProjectStatusEnum yourVariable; /*setter and getter of yourVariable*/ }
在控制器中,将YourCommand yourCommand的实例放入模型
在您的jsp页面上,
<form:form method="POST" modelAttribute="yourCommand" ...>
<form:select path="yourVariable" cssErrorClass="yourErrorClass">
<option>Select Project Status</option>
<form:options itemLabel="key" />
</form:select>
</form:form>