code_junkie

iterating over Enum constants in JSP

Nov 11th, 2011
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. package com.example;
  2.  
  3. public enum CoverageEnum {
  4.  
  5. COUNTRY,
  6. REGIONAL,
  7. COUNTY
  8. }
  9.  
  10. <c:forEach var="type" items="<%= com.example.CoverageEnum.values() %>">
  11. ${type}
  12. </c:forEach>
  13.  
  14. <c:forEach var="type" items="${myprefix:getValues()}">
  15.  
  16. public static <T extends Enum<T>> Enum<T>[] getValues(Class<T> klass) {
  17. try {
  18. Method m = klass.getMethod("values", null);
  19. Object obj = m.invoke(null, null);
  20. return (Enum<T>[])obj;
  21. } catch(Exception ex) {
  22. //shouldn't happen...
  23. return null;
  24. }
  25. }
  26.  
  27. <form:form method="post" modelAttribute="cluster" cssClass="form" enctype="multipart/form-data">
  28. <form:label path="clusterType">Cluster Type
  29. <form:errors path="clusterType" cssClass="error" />
  30. </form:label>
  31. <form:select items="${clusterTypes}" var="type" path="clusterType"/>
  32. </form:form>
  33.  
  34. @ModelAttribute("clusterTypes")
  35. public MyClusterType[] populateClusterTypes() {
  36. return MyClusterType.values();
  37. }
Add Comment
Please, Sign In to add comment