JSF - Como recuperar os dados selecionados num SelectManyCheckBox?

4 respostas
Psycopata

Pois é, como eu faço isso? Esse é o mesmo problema do seguinte tópico: http://www.guj.com.br/posts/list/92383.java , porém ele está sem solução.

Bom, eu na minha aplicação conforme o exemplo desta página: http://www.javabeat.net/tips/49-hselectmanycheckbox-java-server-faces-jsf.html , porém não funcionou.

Vou postar os meus códigos:

trecho JSP

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>

<h:panelGrid columns="3" border="0" cellpadding="0" cellspacing="0" width="100%">
    <h:selectOneMenu value="#{cadastroEvt.codEstado}" onchange="submit()">
        <f:selectItems value="#{cadastroEvt.listaEstatos}"/>
    </h:selectOneMenu>
    <f:verbatim>
        <div style="overflow:auto;width:250px;height:200px">
            <h:selectManyCheckbox value="#{cadastroEvt.municipios}" layout="pageDirection">
                <f:selectItems value="#{cadastroEvt.listaMunicipiosPorUf}"/>
            </h:selectManyCheckbox>
        </div>
    </f:verbatim>
    <h:selectManyListbox value="#{cadastroEvt.removeMunic}">
        <f:selectItems value="#{cadastroEvt.listaMunicSelecionados}"/>
    </h:selectManyListbox>
    <f:verbatim></f:verbatim>
    <h:commandButton value="Adicionar" action="#{cadastroEvt.adicionarMunicipios}"/>
    <h:commandButton value="Remover" action="#{cadastroEvt.removerMunicipios}" />
</h:panelGrid>

Trechos Java

//declaração
 private List<Integer> municipios;

//gettes and setters
public List<Integer> getMunicipios()
    {
        return municipios;
    }


    public void setMunicipios(List<Integer> municipios)
    {
        this.municipios = municipios;
    }

//função que é acionada pelo botão:
public void adicionarMunicipios()
    {
        ArrayList<Integer> lst = new ArrayList<Integer>();
        for (int i = 0; ( (municipios != null) && (i < municipios.size()) ); i++)
            lst.add( municipios.get(i) );
        for (int i = 0;( (municSelec != null) && (i < municSelec.size() ) ); i++)
            lst.add( municSelec.get(i) );
        if (lst != null)
        municSelec = lst;
        municipios = null;
    }

Bom, eu não estou conseguindo capturar os dados marcados no checkbox. Ele sequer entra no set

4 Respostas

andre.santos

Pelo oq pude ver, o setMunicipios recebe um List de Integers… O q vc passa no getter do listaMunicipiosPorUf?

Você tem q passar um Integer como value do f:selectedItems. No caso deveria ser algo como:

public Map&lt;String,Integer&gt; getListaMunicipiosPorUf(){
    Map&lt;String,Integer&gt; mapUf = new HashMap&lt;String,Integer&gt;();
    mapUf.put("Acre",new Integer(1)); //Ou mapUf.put("Acre",1); ja que o Java5 fz cast automaticamente.

    return mapUf;
}

Agora não me lembro se é <Label,Value> ou <Value, Label> no map. Qqr coisa tenta inverter os tipos no Generic do Map e Hashmap.

Espero ter ajudado.
Abraço

Psycopata

Bom, na verdade eu nunca usei map para esses objetos JSF. Eu uso o collection.

Vou postar então o método getListaMunicipiosPorUf():

public Collection<SelectItem> getListaMunicipiosPorUf()
    {
        Collection<SelectItem> rtn = new ArrayList<SelectItem>();
        MunicipioDAO dao = new MunicipioDAO();
        dao.setCodUf( codEstado );
        MunicipioBean [] mun = dao.municCodUf();
        for (int i = 0; i < mun.length; i++)
            rtn.add( new SelectItem( new Integer( mun[i].getCd_ibge() ), mun[i].getNm_mun()) );
        return rtn;
    }
andre.santos

Não sei se tem haver, mas já tentou mudar

public List&lt;Integer&gt; getMunicipios(){  
    return municipios;  
}  

public void setMunicipios(List&lt;Integer&gt; municipios){  
    this.municipios = municipios;  
}

para

public Collection&lt;Integer&gt; getMunicipios(){  
    return municipios;  
}  

public void setMunicipios(Collection&lt;Integer&gt; municipios){  
    this.municipios = municipios;  
}

Pq Collection não é List, mas List extends Collection.

Psycopata

Vou tentar

Criado 27 de fevereiro de 2009
Ultima resposta 3 de mar. de 2009
Respostas 4
Participantes 2