Error accessing method in Managedbean

Asked

Viewed 135 times

1

Guys I’m having problems accessing a method that is in my Managedbean, the idea is the following: I have a page where I list the records that are in db from the click on the "Query" button and then the records appear, all in a good way but 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 I specified. Please help me on this, and for the record I am beginner with JSF, 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 in 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 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;      
}

1 answer

2

Your problem is here:

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

You should not call methods the 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 do the removal you want, you need to inform the item to be removed on MBean. One way to do this is by using the tag f:setPropertyActionListener as follows:

1 - Within the commandButton the tag that maps a value to an Mbean target variable will be added:

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

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

class ControllerAluno {
    private Aluno alunoRemovido;

    public Aluno getAlunoRemovido() { return alunoRemovido; }

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

3 - Modify the method removerAluno 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 the so-called methods of Managed Beans, it is always necessary to create a way to transmit values because the calls to methods cannot transmit parameters. There are other ways to do it, I used the f:setPropertyActionListener but another form of value "injection" could be used.

  • Giuliana thanks for the corrections, I’m still getting there I’m kind of lost, but regarding the method I still have a problem that I do not understand why it happens. When I click the remove button, at least it was to appear the "Signed in" on the console, but no, by clicking remove it simply back to the page I do the listing, I don’t know if you understood what I meant but summarizing, i cannot access this and no other method by clicking the remove.

  • This is because of the invalid method calls. Changing the calls as I told you don’t work? Something else, are you using jsf 2.0? Your Mbeans are annotated or mapped in the settings?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.