I'm new to JSF and I'm working on a small project with one JPA entity, but today working on the project I had several doubts and some problems. All the knowledge I have of JSF It's from books.
The problem is the following:
When I click the list all button the list loads fine and show the data that I have in the DB, but when I click the delete button in the column of a row it does nothing, it seems like refresh the page. If I change the scope of the bean to SessionScope it works fine. Most of the books that I read show examples of CRUD operations with RequestScope, and some other internet articles show the same examples.
The create and update operation it's handled in another form in another xhtml pages (crearBeneficiario.xhtml and actualizarBeneficiario.xhtml) and works fine.
I'm using JSF 2.3 on Payara Server 5.192 and Eclipse IDE 4.12
If you could lead me on the right path, I would greatly appreciate it.
I attach some images Image 1 before delete click Image 2 after delete click
In my listarBeneficiario.xhtml (Partial code) i have to following forms and datatable
<h:form>
<button type="submit" class="btn btn-block btn-sm" jsf:action="#{navigationController.goCrearBeneficiario()}">Nuevo beneficiario</button>
<hr />
<button type="submit" class="btn btn-block btn-sm" jsf:action="#{beneficiarioController.listBeneficiariosTodos()}">Listar Todos</button>
<hr />
</h:form>
<h:form>
<h:dataTable value="#{beneficiarioController.beneficiarios}" var="b" styleClass="table table-hover"headerClass="verderuralp-color white-text">
<h:column>
<f:facet name="header">Nombre</f:facet>
<h:outputText value="#{b.nombre}" />
</h:column>
<h:column>
<f:facet name="header">A. Paterno</f:facet>
<h:outputText value="#{b.apellidoPaterno}" />
</h:column>
<h:column>
<f:facet name="header">A. Materno</f:facet>
<h:outputText value="#{b.apellidoMaterno}" />
</h:column>
<h:column>
<f:facet name="header">CURP</f:facet>
<h:outputText value="#{b.curp}" />
</h:column>
<h:column>
<h:commandButton action="#{beneficiarioController.deleteBeneficiario(b.beneficiarioId)}" value="Eliminar"/>
</h:column>
</h:form>
And this is my CDI Bean (Partial code)
@Data
@NoArgsConstructor
@Named
@RequestScoped
public class BeneficiarioController
implements Serializable
{
private static final long serialVersionUID = 1L;
private String mensaje;
private String desde;
private String hasta;
private String busqueda;
private BeneficiarioDto beneficiarioDto = new BeneficiarioDto();
private List<Beneficiario> beneficiarios = new ArrayList<Beneficiario>();
@EJB
private BeneficiarioService beneficiarioService;
public void deleteBeneficiario( Beneficiario b ) {
try {
beneficiarioService.deleteBeneficiario( b.getBeneficiarioId() );
mensaje = "El beneficiario fue dado de baja";
} catch ( EntidadNoEncontrada e ) {
mensaje = e.getMessage();
}
beneficiarios = beneficiarioService.listBeneficiariosPorEstado( true );
}
public void listBeneficiariosTodos() {
this.beneficiarios = beneficiarioService.listBeneficiariosPorEstado( true );
}