How to make 3 SelectOneMenu nested?

8

I'm creating a page that has to have 3 SelectOneMenu of primefaces. In the first one I will load a center (of the university), the second will carry a edital and the third would load the disciplines of this edital.

The first SelectOneMenu works, when change of option it loads only the edicts of that option. But when I change the option of the second SelectOneMenu it no longer calls the method. Why is not he calling the method? What is wrong? Here are the codes:

deferirInscricoes.xhtml

<h:form id="formInscricoes">

    <p:selectOneMenu id="selectCentro" value="#{inscricaoBean.centroSelecionado}" converter = "centroConverter">
        <f:selectItem itemValue="#{null}" itemLabel="- Selecione um Centro -" />
        <f:selectItems value="#{inscricaoBean.listaCentros}" var="centro" itemValue="#{centro}" itemLabel="#{centro.nome}"/>
        <p:ajax event="change" process="@this" listener="#{inscricaoBean.carregaEditais}" update="selectEdital" />
    </p:selectOneMenu>

    <p:selectOneMenu id="selectEdital" value="#{inscricaoBean.editalSelecionado}" converter = "editalConverter">
        <f:selectItem itemValue="#{null}" itemLabel="- Selecione um Edital -" />
        <f:selectItems value="#{inscricaoBean.listaEditaisPorCentro}" var="edital" itemValue="#{edital}" itemLabel="#{edital.titulo}"/>
        <p:ajax event="change" process="@this" listener="#{inscricaoBean.carregaDisciplinas}" update="selectDisciplinas" />
    </p:selectOneMenu>

    <p:selectOneMenu id="selectDisciplinas" value="#{inscricaoBean.disciplinaSelecionada}" converter = "disciplinaConverter">
        <f:selectItem itemValue="#{null}" itemLabel="- Selecione uma Disciplina -" />
        <f:selectItem itemValue="#{null}" itemLabel="- Teste -" />
        <f:selectItems value="#{inscricaoBean.listaDisciplinaPorEdital}" var="disciplina" itemValue="#{disciplina}" itemLabel="#{disciplina.nome}"/>
    </p:selectOneMenu>  
 <h:form>

inscriptionBean.java

private List<Centro> listaCentros;
private List<Edital> listaEditaisPorCentro;
private List<Disciplina> listaDisciplinaPorEdital;
CentroDAO centroDAO = new CentroDAO();
EditalDAO editalDAO = new EditalDAO();
DisciplinaDAO disciplinaDAO = new DisciplinaDAO();
private Centro centroSelecionado;
private Edital editalSelecionado;
private Disciplina disciplinaSelecionada;

// Todos os getters e setters acima foram criado no padrão.

public void carregaEditais() {
    System.out.println("Editais carregados.");
    listaEditaisPorCentro = editalDAO.getListaEdital(centroSelecionado);
}

public void carregaDisciplinas() {
    System.out.println("Disciplinas carregadas.");
    listaDisciplinaPorEdital = disciplinasDAO.getListaDisciplinaPorEdital(edital);
}

}

CentroConverter.java

@FacesConverter("centroConverter")
public class centroConverter implements Converter {

    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            try {
                CentroDAO centroDAO = new CentroDAO();
                Integer id = Integer.parseInt(value);
                Centro centro = centroDAO.getCentro(id);
                return centro;

            } catch(Exception e) {
                System.out.println("Problema no centroConverter.!");
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro de conversão", "Not a valid theme."));
            }
        }
        else {
            return null;
        }
    }

    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
            Centro c = new Centro();
            c = (Centro) object;
            return ""+c.getIdCentro();
        }
        else {
            return null;
        }
    }   
}     

EditalConverter.java

@FacesConverter("editalConverter")
public class editalConverter implements Converter {

    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            try {
                EditalDAO editalDAO = new EditalDAO();
                Integer id = Integer.parseInt(value);
                Edital edital = editalDAO.getEdital(id);
                return edital;

            } catch(Exception e) {
                System.out.println("Problema no editalConverter.!");
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro de conversão", "Not a valid theme."));
            }
        }
        else {
            return null;
        }
    }

    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
            Edital e = new  Edital();
            e = (Edital) object;
            return ""+e.getIdEdital();
        }
        else {
            return null;
        }
    }   
}     

DisciplineConverter.java

@FacesConverter("disciplinaConverter")
public class disciplinaConverter implements Converter {

    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            try {
                DisciplinaDAO disciplinaDAO = new DisciplinaDAO();
                Integer id = Integer.parseInt(value);
                Disciplina disciplina = disciplinaDAO.getDisciplina(id);
                return disciplina;

            } catch(Exception e) {
                System.out.println("Problema no disciplinaConverter.!");
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro de conversão", "Not a valid theme."));
            }
        }
        else {
            return null;
        }
    }

    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
            Disciplina d = new Disciplina();
            d = (Disciplina) object;
            return ""+d.getIdDisciplina();
        }
        else {
            return null;
        }
    }   
}     

The first method it calls, loads the list. But the second does not. I put a print just to test, but the method is not being called.

    
asked by anonymous 07.07.2015 / 05:05

2 answers

1

I know the post is old, but maybe someone is going through a similar problem and it's a very important tip:

I was with similar problem and I broke my head I went on several post on the net and the problem was actually in the relationship between Model X converter x selectOneMenu , so that it works correctly needs the "Models "has the methods equals () and hascode () implemented , in case this post would implement in the classes Center, Edital and Discipline.

The fact of not having these methods implemented, can generate an error in the validation phase of the JSF preventing the normal sequence of phases.

    
26.10.2015 / 15:23
0

This listener valueChangeListener is only called when the user selects an option, and you did not give an update on your second select, so it will not load the new information.

My suggestion add in your first select:

<p:ajax event="change" process="@this" listener="{inscricaoBean.clearEdital}" update="ID_DO_PROX_SELECT" />

remove valueChangeListener="#{inscricaoBean.clearEdital}" and change your method:

public void clearEdital(){
    listaEditalPorCentro = editalDAO.getListaEdital();
    System.out.println("Centro: "+centroSelecionado);
    System.out.println("Edital: "+listaEditalPorCentro);

}

Your selectedSite will already have the new value because of process="@this"

    
07.07.2015 / 20:36