How could I display the values of enum structure in JSP? I use the Spring MVC to implement my project.
Many thanks!
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;
}
}
Add the enum values in an attribute of your request:
// ProjectStatusEnum.values() return an array of ProjectStatusEnum
request.setAttribute("enum", ProjectStatusEnum.values());
And finally, within your JSP:
<ul class="dropdownmenu">
<c:forEach items="${enum}" var="entry">
<li>${entry.key} (${entry.value})</li> <!-- for example -->
</c:forEach>
</ul>
Define in your ModelAttribute class the variable to bind to the type of your ENUM class
\n public class YourCommand implements Serializable{ \n private ProjectStatusEnum yourVariable; \n /*setter and getter of yourVariable*/ \n } \n
In the controller, put an instance of YourCommand yourCommand into Model
On your jsp page,
<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>
\n<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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.