Kleber Mota Kleber Mota - 1 year ago 47
Javascript Question

Iterating multiple items of different sizes in JSTL

My JSP page is receiving two lists: One is the list of all autorizations available and the other is the autorizations availables for one specific user. I want create a list of checkbox where all the autorizations are listed, but only the autorizations of the user are checked. Someone can point me one direction for do this?


this is the html/jstl code to display ALL the autorizations:

<div class="row" id="autorizacoes ${item.id}">
<c:forEach var="item_auth" items="${autorizacoes}">
<input type="checkbox" name="${item_auth.nome}" onclick="mudaAutorizacao(${item_auth.nome})">${item_auth.nome}

the page where this code is included is invoked by this method (placed in my controller):

public ModelAndView listagem_usuario()
UsuarioDAO lista = new UsuarioDAO();
DadosDAO dados = new DadosDAO();
TipoDAO tipo = new TipoDAO();
AutorizacaoDAO auth = new AutorizacaoDAO();

ModelAndView mav = new ModelAndView();
mav.addObject("usuarios", lista.lista());
mav.addObject("campos", dados.getListaCampos());
mav.addObject("tipos", tipo.getListaTipos());
mav.addObject("autorizacoes", auth.getListaAutorizacoes());

return mav;

that is the method from AutorizationDAO which read the autorizations from the database:

public List<Autorizacao_usuario> getListaAutorizacoes()
List<Autorizacao_usuario> lista = new ArrayList<Autorizacao_usuario>();


if(executa("SELECT id, nome, descricao FROM autorizacoes;")) {
Autorizacao_usuario auth = new Autorizacao_usuario(getInt(1), getString(2), getString(3));

return lista;

the autorizations of each user are available from this
List<Autorizacao_usuario> getAutorizacoes()
(of the class com.HorarioLivre.core.Usuario):

and, finally, this is the POJO class that keeps the autorization data in memory:

package com.HorarioLivre.core;

public class Autorizacao_usuario {
private int Id;

private String nome;

private String descricao;

public Autorizacao_usuario()

public Autorizacao_usuario(int id, String nome, String descricao)

public int getId() {
return Id;

public void setId(int id) {
Id = id;

public String getNome() {
return nome;

public void setNome(String nome) {
this.nome = nome;

public String getDescricao() {
return descricao;

public String setDescricao(String descricao) {
this.descricao = descricao;
return descricao;

Answer Source

try something like this:

<div class="row" id="autorizacoes ${item.id}">
        <c:forEach var="item_auth" items="${autorizacoes}">
            <c:set var="isChecked" value="${false}"/>
            <c:forEach var="user_auth" items="${userAutorizacoes}">
                <c:if test="${user_auth.id == item_auth.id}">  <!-- here compare according ID or any unique attribute -->
                    <c:set var="isChecked" value="${true}"/>
            <input type="checkbox" name="${item_auth.nome}" onclick="mudaAutorizacao(${item_auth.nome})"  <c:if test="${isChecked}">checked="checked"</c:if> />${item_auth.nome}

It checks for each item_auth if it equals to autorizations of the user and if so it sets checked attribute in the checkbox element.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download