0

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 );
    }
  • Start here: https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value – Kukeltje Aug 03 '19 at 05:41
  • @Kukeltje thanks for your comment, I read the question and I think that my problem is the point number 4 in that post, but now I have a doubt in how to implement the PostConstruct annotation, it is no clear to me how to load the list in that annotation mainly because I have 4 different queries to populate that list – Rene Nochebuena Aug 03 '19 at 06:02
  • Therefore always create an [mcve] of your problem. – Kukeltje Aug 03 '19 at 06:08
  • 1
    Using it the way you do is still possible by using a sope in between request and session: viewscoped. Perfect fit for this – Kukeltje Aug 03 '19 at 06:15
  • Is in my first code block in the second form, the point number 4 says `If UICommand or UIInput components are placed inside an iterating component like , , etc, then you need to ensure that exactly the same value of the iterating component is been preserved`, then says `making sure that you load the data model in PostConstruct of the bean` and I dont have a PostConstruct annotation – Rene Nochebuena Aug 03 '19 at 06:18
  • @Kukeltje I change my bean to the ViewScope and made some tweaks works like a charm! thanks – Rene Nochebuena Aug 03 '19 at 06:29

1 Answers1

0

The answer is like @Kukeltje said

Using it the way you do is still possible by using a sope in between request and session: viewscoped. Perfect fit for this