Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6188515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:14:41+00:00 2026-05-24T02:14:41+00:00

I need to display all values of an enum as <option> elements. I have

  • 0

I need to display all values of an enum as <option> elements. I have achieved this using scriptlets:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="errors" tagdir="/WEB-INF/tags/jostens/errors" %>
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

...
<%
        Class<?> c = CarrierCode.class;
        for (Object carrier : c.getEnumConstants()) {
            CarrierCode cc = (CarrierCode) carrier;
            StringBuilder sb = new StringBuilder();
            Formatter formatter = new Formatter(sb, Locale.US);
            out.print(formatter.format("<option value='%s'>%s</option>\n", cc.getMfCode(), cc.name()));
        }
%>
...

However, I would like to implement it using JSTL/EL code instead. How can I do it?

UPDATE:

Spring has a much easier way to do this now. First add the spring frame work tags
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
then if you just declare a select where the variable in path is an Enum,
spring automagically finds the other elements.

<form:select path="dataFormat.delimiter" class="dataFormatDelimiter">
    <form:options items="${dataFormat.delimiter}"/>
</form:select>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:14:42+00:00Added an answer on May 24, 2026 at 2:14 am

    Create a ServletContextListener implementation which puts the enum values in the application scope during webapp startup so that it’s available in EL by ${carrierCodes}. This class is reuseable for all other things you’d like to do once during webapp’s startup.

    @WebListener
    public class Config implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            event.getServletContext().setAttribute("carrierCodes", CarrierCode.values());
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            // NOOP
        }
    
    }
    

    Note that I used Enum#values() instead of the clumsy Class#getEnumConstants() method. It returns an array of all enum values.

    Then, in JSP, just use JSTL <c:forEach> to iterate over it.

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    ...
    <select name="carrierCode">
      <c:forEach items="${carrierCodes}" var="carrierCode">
        <option value="${carrierCode.mfCode}">${carrierCode}</option>
      </c:forEach>
    </select>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

through jsonparsing i parse all the string values again i need to display the
I need to display 2d images in opengl using textures. The image dimensions are
I have to make a string by using the values which the user selects
I am running a Tomcat application, and I need to display some time values.
I have come unstuck with this goal after successfully using a dataModel when rendering
Scenario: I need to display the average of the last 20 reported values. I
I have a database table with images that I need to display. In my
i am developing one application with map view i need display the weather depends
I need to display an error message on rejecting a drop in my application.
I need to display many pages of news in a site. Should I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.