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>
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.