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;
}
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.
– Carlos
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?
– Giuliana Bezerra