如何在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;
}
}
在请求的属性中添加枚举值:
// 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>
-
在ModelAttribute类中定义要绑定到ENUM类类型的变量
<>之前实现Serializable{private ProjectStatusEnum/* yourVariable的setter和getter */} -
在控制器中,将YourCommand的实例YourCommand放入Model
-
在您的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>