• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you iterate over an Enum with JSTL?  RSS feed

 
Angus McIntosh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was driving me nuts! After extensive googling, I at least know now that Java 5 enums are not supported by the JSTL standard, but that still leaves me with an enum to iterate over.

So, I have an Enum called Levels, which has getLevelName(), getAltLevelName() and getOrdinal() methods (if I can remove the getOrdinal() method and just use the regular ordinal() method I'd be happy!).

What I'd love to do is:



Of course this won't work, but is there a way to iterate over this Enum without cheating (i.e. resorting to scriptlets)?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can:
  • Use the values() method of the Enum in the page controller fo the JSP to fetch an array of the enum values to send as a scoped variable to the JSP

  • or
  • Create a custom tag to do the same

  • or
  • Create an EL function to do the same


  • With the above three mechanisms, it is never necessary to "cheat" and use scriptlets for anything that should be done in a JSP.
     
    Angus McIntosh
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks!

    #1 is what I went with.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!