I need to treat an enumerated as if it were an array in a jsp with jstl

2

For now I am loading the elements of the list manually, but I would like to know if you can go through any of the components provided by JSTL ( c:forEach for example), because in the enum I have 3 options, but imagine if there are 500 options ...

This is the enum:

public class Alumno extends Usuario implements Serializable {

    public enum Genero {
        HOMBRE,
        MUJER,
        OTRO;
//UPDATEADO
//Ya que no se pueden llamar a métodos estáticos desde un jsp es necesario crear éste método para acceder a el desde el jsp y obtener un array
        public Genero[] getValues() {
            return Genero.values();
        }
        }
        @Column(name = "Genero", length = 6)
        @Enumerated(EnumType.STRING)
        private Genero genero;
    }

Now I have it like this (manual form):

<select class="form-control" id="generoEnum" name="generoEnum" title="Género">
                <c:choose>
                    <c:when test="${usuario.genero=='HOMBRE'}">
                        <option selected="" value="hombre">Hombre</option>
                        <option value="mujer">Mujer</option>
                        <option value="otro">Otro</option>
                    </c:when>
                    <c:when test="${usuario.genero=='MUJER'}">
                        <option value="hombre">Hombre</option>
                        <option selected="" value="mujer">Mujer</option>
                        <option value="otro">Otro</option>
                    </c:when>
                    <c:when test="${usuario.genero=='OTRO'}">
                        <option value="hombre">Hombre</option>
                        <option value="mujer">Mujer</option>
                        <option selected="" value="otro">Otro</option>
                    </c:when>
                    <c:otherwise>
                        <option value="hombre">Hombre</option>
                        <option selected="" value="mujer">Mujer</option>
                        <option value="otro">Otro</option>
                    </c:otherwise>
                </c:choose>
            </select>

I tried to do it with my experiments but without success ... Thanks in advance.

EDIT: to close a little more what I'm looking for is to access the enum Genero from the jsp, something like that (See the companion solution @Klaimmore

<c:forEach items="${Alumno.Genero}" var="genero">
       <option value="${genero}">${genero.simpleName()}</option>
</c:forEach>

EDITO2: "user" is a Student object that is in session.

UPDATE to put the trace: When I login in the application a session object (sessionScope) is loaded under the name "user", then I click on a link in the side menu to load through AJAX a page in a div on the right:

//** cargamos un jsp genericamente en el panel de la derecha **//
    $(".opcion").click(function () {
        $.get("jsp/opciones_paneles/" + $(this).attr("id"), function (data, status) {
            $("#right-panel").empty();
            $("#right-panel").append(data);

            //lamamos al javascript de nuevo
             calljavascript();
        }); 
    });

Then a jsp containing the following JSTL is loaded (updated with the possible solution of the partner @Klaimmore):

<select class="form-control" id="generoEnum" name="generoEnum" title="Género">
                <c:forEach items="${Alumno.Genero.getValues()}" var="sexo">
                    <c:choose>
                        <c:when test="${usuario.genero eq sexo}">
                            <option value="${sexo}" selected>${sexo.name()}</option>
                        </c:when>
                        <c:otherwise>
                            <option value="${sexo}">${sexo.name()}1</option>
                        </c:otherwise>
                    </c:choose>
                </c:forEach>
            </select>

The final view would be this:

As you can see, the other fields are filled with JSTL alone

SOLUTION

Thanks to @Klaimmore for the solution!

The exercise tries to show with a Java enum in a JSP, in my case it will be displayed in a select

  • We add a getValues() method in the desired enum

    public class Alumno extends Usuario implements Serializable, Comparable<Alumno> {
    
    public enum Genero {
        HOMBRE,
        MUJER,
        OTRO;
    
        public Genero[] getValues() {
            return Genero.values();
        }
    }
    @Column(name = "Genero", length = 6)
    @Enumerated(EnumType.STRING)
    private Genero genero;
    

    }

  • We introduce the in the desired JSP

  • (I apologize if it bothers the code in the image, it is the same piece as below but if I remove the image the code is not displayed to me: /)

    <select class="form-control" id="generoEnum" name="generoEnum" title="Género">
                <c:forEach items="${usuario.genero.values}" var="sexo">
                    <c:choose>
                        <c:when test="${usuario.genero eq sexo}">
                            <option value="${sexo}" selected>${sexo.name()}</option>
                        </c:when>
                        <c:otherwise>
                            <option value="${sexo}">${sexo.name()}</option>
                        </c:otherwise>
                    </c:choose>
                </c:forEach>
            </select>
    

    Here is a note:

    • user = java object Student saved in sessionScope

    The girl in this piece is the:

    (Again apologize, I can not see the code :()

    This string calls the method we created in the Java enum

    I hope it serves you as much as I do, greetings.

        
    asked by Panbo 27.03.2018 в 20:24
    source

    1 answer

    1

    To answer this somewhat old question. You can not access static methods using EL , if you can if you allow yourself to use scriptlets :

    <c:forEach items="<%= jsp.ejemplo.Alumno.Genero.values() %>" var="genero">
        <option value="${genero}">${genero.name()}</option>
    </c:forEach>
    

    How to use scriptlets is considered a bad practice, your other option is to provide an instance method to get the list of values of enum , for example in the same enum :

    public class Alumno extends Usuario implements Serializable {
    
        public enum Genero {
            HOMBRE,
            MUJER,
            OTRO;
    
            public Genero[] getValues() {
                return Genero.values();
            }
        }
        @Column(name = "Genero", length = 6)
        @Enumerated(EnumType.STRING)
        private Genero genero;
    }
    

    And then use it from a valid instance like:

    <c:forEach items="${alumno.genero.values}" var="genero">
        <option value="${genero}">${genero.name()}</option>
    </c:forEach>
    

    With that, your code to show the option selected would be simplified enough:

    <select class="form-control" id="generoEnum" name="generoEnum" title="Género">
        <c:forEach items="${alumno.genero.values}" var="genero">
            <c:choose>
                <c:when test="${alumno.genero eq genero}">
                    <option value="${genero}" selected>${genero.name()}</option>
                </c:when>
                <c:otherwise>
                    <option value="${genero}">${genero.name()}</option>
                </c:otherwise>
            </c:choose>
        </c:forEach>
    </select>
    
        
    answered by 25.04.2018 / 22:27
    source