Fala galera, estou com um problema que não consigo resolver, estou fazendo um projeto com primefaces, um cadastro simples, e tenho um combo com estados e outro com cidades, onde quero selecionar o estado no primeiro combo, e o segundo deve mostrar apenas as cidades referentes ao estado selecionado. Estou me baseando na demo no site do primefaces, mas não tá dando certo, se alguem poder ajudar…
Os combos:
<h:outputText value="Estado:" />
<p:selectOneMenu id="estado" value="#{ClienteBean.cliente.codEstado}" height="120" style="width: 150px" required="true" requiredMessage="Selecione o Estado">
<f:selectItem itemLabel="Selecione" itemValue=""/>
<f:selectItems value="#{CidadeUFComboBean.UFs}"/>
<p:ajax update="cidade" listener="#{CidadeUFComboBean.changeUF}"/>
</p:selectOneMenu>
<h:outputText value="Cidade:" />
<p:selectOneMenu id="cidade" value="#{ClienteBean.cliente.codCidade}" height="120" style="width: 150px" required="true" requiredMessage="Selecione a Cidade">
<f:selectItem itemLabel="Selecione" itemValue=""/>
<f:selectItems value="#{CidadeUFComboBean.cidades}"/>
</p:selectOneMenu>
Meu managedBean:
public class CidadeUFComboBean implements Serializable {
CidadeDAO cidDao = new CidadeDAO();
EstadoDAO ufDao = new EstadoDAO();
/*
* Construtor
*/
public CidadeUFComboBean() {
populateUFs();
populateCidades();
}
/*
* Propriedades
*/
private String uf;
public String getUF() {
return uf;
}
public void setUF(String uf) {
this.uf = uf;
}
private String cidade;
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
this.cidade = cidade;
}
private SortedMap<Integer, String> ufs =
new TreeMap<Integer, String>();
public SortedMap<Integer, String> getUFs() {
return ufs;
}
public void setUFs(SortedMap<Integer, String> ufs) {
this.ufs = ufs;
}
private SortedMap<Integer, SortedMap<Integer, String>> cidadesData =
new TreeMap<Integer, SortedMap<Integer, String>>();
public SortedMap<Integer, SortedMap<Integer, String>> getCidadesData() {
return cidadesData;
}
public void setCidadesData(SortedMap<Integer, SortedMap<Integer, String>> cidadesData) {
this.cidadesData = cidadesData;
}
private SortedMap<Integer, String> cidades = new TreeMap<Integer, String>();
public SortedMap<Integer, String> getCidades() {
return cidades;
}
public void setCidades(SortedMap<Integer, String> cidades) {
this.cidades = cidades;
}
/*
* Helpers
*/
private void populateUFs(){
try {
System.out.println("Populando Ufs...");
List<Estado> estados = ufDao.getEstado();
for (Estado estado : estados) {
ufs.put(estado.getCodEstado(), estado.getUf());
}
System.out.println("Populando Ufs com " + ufs.size() + " itens...");
} catch (SQLException ex) {
Logger.getLogger(CidadeUFComboBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void populateCidades() {
for (int codEstado : ufs.keySet()) {
try {
cidadesData.put(codEstado, cidDao.getCidade(codEstado));
} catch (SQLException ex) {
Logger.getLogger(CidadeUFComboBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
/*
* Action handlers
*/
public void changeUF() {
if (uf != null && !uf.equals("")) {
cidades = cidadesData.get(uf);
} else {
cidades = new TreeMap<Integer, String>();
}
}
}
No combo so mostra o codigo do estado, e nem altera o combo de cidades…
