preRenderView not working during a method call

50 Views Asked by At

I have in my JSF 2.2 page an event listener in the <f:metadata> and its preRenderView is not working on every page call, it's only working for the first page load and when I use the add() method the lists refreshes at shows the added line, but I can't get it to work for my delete() method at all, nothing gets deleted and the view doesn't refresh either.

But everything works fine if I ignore the meta tag and just load the list in the getListe() method, but I shouldn't put business code in the getter.

profile.xhtml

<!DOCTYPE html>
<html lang="fr"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <f:metadata>
            <f:event type="preRenderView" listener="#{profileBean.loadFermes}" />
    </f:metadata>
    <h:head>
        <title>SUCCES</title>
    </h:head>

    <h:body>

        <ui:fragment rendered= "#{!loginBean.loggedIn}">
        Not logged !
        </ui:fragment>
        <ui:fragment rendered= "#{loginBean.loggedIn}">
        Welcome : #{loginBean.user.nom} <br />
        E-mail : #{loginBean.user.email} <br />
        <br />


        <h:form id="form">
            <h:panelGroup id="wrapper">

                <h:outputLabel for="nomFerme">Nom Ferme<span class="requis"></span></h:outputLabel>
                <h:inputText id="nomFerme" value="#{profileBean.ferme.nom_ferme}" size="20" maxlength="20" />
                <h:messages globalOnly="true" infoClass="info" />
                <h:commandButton value="Add" action="#{profileBean.addFerme}" styleClass="sansLabel">
                    <f:ajax execute="@form" render="@form" />
                </h:commandButton>
                <br />

            <h:dataTable id="ferme-table" value="#{profileBean.liste}" 
                         var="f" rules="all" cellpadding="4" cellspacing="0">            

                <f:facet name="header">
                  <h:outputText value="Ferme List" />
                </f:facet> 

                <h:column>
                  <f:facet name="header">
                  <h:outputText value="Ferme Id" />
                  </f:facet> 
                   <h:outputText value="#{f.id_ferme}" />
                </h:column>

                <h:column>
                  <f:facet name="header">
                  <h:outputText value="Name"/>
                  </f:facet> 
                   <h:outputText value="#{f.nom_ferme}"  />
                </h:column> 

                <h:column>
                  <f:facet name="header">
                  <h:outputText value="User Id"/>
                  </f:facet> 
                   <h:outputText value="#{f.utilisateur.id}"  />
                </h:column>                         

                <h:column>
                  <f:facet name="header">
                  <h:outputText value="Action"/>
                  </f:facet>
                     <h:panelGroup class="action" >
                        <h:commandLink value="Delete" action="#{profileBean.deleteFerme(f)}" >
                        <f:ajax execute="@form" render="@form" />
                        </h:commandLink>

                        <h:link outcome="editFerme" value="Edit" includeViewParams="true">
                            <f:param name="fermeId" value="#{f.id_ferme}"></f:param>
                        </h:link>
                    </h:panelGroup>
                </h:column>
            </h:dataTable>
            </h:panelGroup>
        </h:form>
        </ui:fragment>

    </h:body>
</html>

profileBean :

@Named
@RequestScoped
public class ProfileBean implements Serializable{

    private static final long serialVersionUID = 1L;

    // injecting the SessionScoped Bean containing the logged User object
    @Inject
    private LoginBean loginBean;

    private Ferme ferme;
    private List<Ferme> liste;

    public Ferme getFerme() {
        return ferme;
    }

    public void setFerme(Ferme ferme) {
        this.ferme = ferme;
    }

    public List<Ferme> getListe() {
        return liste;
    }

    public void setListe(List<Ferme> liste) {
        this.liste = liste;
    }

    // The loadFermes method called in the Event Listener
    public void loadFermes() {
        liste = loginBean.getUtilisateurDao().lister(loginBean.getUser());
    }

    public void addFerme() {
        ferme.setUtilisateur(loginBean.getUser());
        loginBean.getUtilisateurDao().creerFerme(ferme);
    }

    // This is the delete method called in the view
    public void deleteFerme(Ferme ferme) {
        loginBean.getUtilisateurDao().supprimerFerme(ferme);
    }


    public void findFerme() {
        this.ferme = loginBean.getUtilisateurDao().findFermeById(this.ferme.getId_ferme());
    }

    public String edit() {
        ferme.setUtilisateur(loginBean.getUser());
        loginBean.getUtilisateurDao().modifierFerme(this.ferme);
        return "profile.xhtml?faces-redirect=true";

    }

    @PostConstruct
    public void init() {
        liste = new ArrayList<Ferme>();
        ferme = new Ferme();
    }

}

UtilisateurDao :

...
...
// The delete method in the Dao is working fine
public void supprimerFerme(Ferme ferme) {
        try {
            Query query = em.createQuery( "DELETE FROM Ferme u WHERE u.id_ferme = :fermeid and u.utilisateur = :user", Ferme.class );
            query.setParameter("fermeid", ferme.getId_ferme());
            query.setParameter("user", ferme.getUtilisateur());
            query.executeUpdate();

        } catch ( Exception e ) {
            throw new DAOException( e );
        }
    }
..
..
..
0

There are 0 best solutions below