• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do you iterate over an Enum with JSTL?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks!

    #1 is what I went with.
    reply
      Bookmark Topic Watch Topic
    • New Topic