Error accessing method in ManagedBean

1

I'm having problems accessing a method that stays in my ManagedBean, the idea is this: I have a page where I can check the records that are in the db from the "Browse" button and then the records appear, all in a good one however for each record I have the button next to "Remove" and "Edit", when I click the remove button it refreshes the page and does not even enter the method that I specified. Please help me in this, and to be a beginner with JSF technologies, Hibernate, etc ...

Listing Page

<h:form>
    <h:commandButton value="Consultar Alunos"
        action="#{controllerAluno.alunosCadastrados}" />
</h:form>

<br />
<p></p>


<h:form>
    <p:dataTable var="item" value="#{controllerAluno.alunos}"
        rendered="#{not empty controllerAluno.alunos}">
        <p:column headerText="Cód. Aluno">
            <center>
                <h:outputText value="#{item.id}" />
            </center>
        </p:column>

        <p:column headerText="Nome">
            <center>
                <h:outputText value="#{item.nome}" />
            </center>
        </p:column>

        <p:column headerText="Data">
            <center>
                <h:outputText value="#{item.data_Nascimento}">
                    <f:convertDateTime pattern="dd, MMMM yyyy" locale="pt_BR"
                        timeZone="" />
                </h:outputText>

            </center>
        </p:column>

        <p:column headerText="Sexo">
            <center>
                <h:outputText value="#{item.sexo}" />
            </center>
        </p:column>

        <p:column headerText="opções">
            <center>
                <h:commandButton value="Editar" />
                ||
                <h:commandButton value="Remover"
                    action="#{controllerAluno.removerAluno(item)}" />
            </center>
        </p:column>
    </p:dataTable>
</h:form>

<h:form>
    <center>
        <h3>
            <h:commandLink value="Cadastrar novo Aluno"
                action="paginaInicial?faces-redirect=true" />
        </h3>
    </center>
</h:form>

Methods Accessed on ManagedBean

Remove:

    public void removerAluno(Aluno alunoSelecionado){
    try {

        EntityManagerFactory factory = Persistence.createEntityManagerFactory("conexaoDB");         
        EntityManager em = factory.createEntityManager();
        EntityTransaction et = em.getTransaction();

        System.out.println("Entrou");
        et.begin();
        Aluno a = em.find(Aluno.class, alunoSelecionado.getId());           
        em.remove(a);           
        System.out.println("Aluno: "+a.getNome()+" removido com sucesso!");
        a =null;
        alunoSelecionado = null;

        em.close();
        factory.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Method that loads the ArrayList

    @SuppressWarnings("unchecked")
public ArrayList<Aluno> alunosCadastrados(){

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("conexaoDB");         
    EntityManager em = factory.createEntityManager();
    EntityTransaction et = em.getTransaction();

    this.alunos =  (ArrayList<Aluno>) em.createQuery("FROM " + Aluno.class.getName()).getResultList();
    em.close();
    factory.close();

    return alunos;      
}
    
asked by anonymous 14.09.2016 / 00:40

1 answer

2

Your problem is here:

<h:commandButton value="Remover"
                action="#{controllerAluno.removerAluno(item)}" />

You should not call methods of Managed Bean passing parameters. The call references only the name of the method that will only have parameters if it is called by an event. To make the removal you want, you must enter the item to remove in MBean . One way to do this is by using the f:setPropertyActionListener tag as follows:

1 - Within the commandButton will be added the tag that maps a value into a target variable of the MBean:

<h:commandButton value="Remover" action="#{controllerAluno.removerAluno}">
    <f:setPropertyActionListener value="#{item}" target="#{controllerAluno.alunoRemovido}" />
</h:commandButton>

2 - Create an attribute in ControllerAluno and its get and set methods so that it is possible to map the student to be removed from view :

class ControllerAluno {
    private Aluno alunoRemovido;

    public Aluno getAlunoRemovido() { return alunoRemovido; }

    public void setAlunoRemovido(Aluno alunoRemovido) { 
       this.alunoRemovido = alunoRemovido;
    }
}

3 - Modify the removerAluno method to reference the created attribute:

public void removerAluno(){
try {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("conexaoDB");         
    EntityManager em = factory.createEntityManager();
    EntityTransaction et = em.getTransaction();

    System.out.println("Entrou");
    et.begin();
    Aluno a = em.find(Aluno.class, alunoRemovido.getId());           
    em.remove(a);           
    System.out.println("Aluno: "+a.getNome()+" removido com sucesso!");
    a =null;
    alunoRemovido = null;

    em.close();
    factory.close();
} catch (Exception e) {
    e.printStackTrace();
}
}

The logic is this for calling Managed Beans methods, it is always necessary to create a way to pass values since method calls can not pass parameters. There are other ways to do it, I used f:setPropertyActionListener but another form of "injection" of values could be used.

    
14.09.2016 / 01:30