0

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 );
        }
    }
..
..
..
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Dwix
  • 1,139
  • 3
  • 20
  • 45
  • 1
    Which JSF impl/version? (given that you're using JSF 2.2, you could also use `` instead) – BalusC Oct 07 '15 at 22:30
  • @BalusC I use MyFaces, and viewAction doesn't work either, the delete method don't get called at all, I have no idea why, I posted the entire code related, please if you have any idea let me know. – Dwix Oct 07 '15 at 23:20
  • PS : Any I put inside the the in the , is not able to call any method, but if I put the button outside the columns tags, and just inside the so just one button is created and not many of them because of the loop, so in this case only it can call any method. – Dwix Oct 07 '15 at 23:40
  • Which JSF impl/version? If you even can't tell it, I suspect classpath pollution with multiple different versioned JSF libraries. – BalusC Oct 08 '15 at 07:33
  • @BalusC Thanks for your reply, I've checked again, here is the full info : package javax.faces.context, JavaServer Faces, version 2.2 // Mojarra // 2.2.7 // Oracle America, Inc. and I've read that commandButtons don't work inside datatables if my bean is a requestScoped, is this really still the reason ? because I got the info from some very old topics. – Dwix Oct 08 '15 at 11:48
  • 1
    Depends on model and logic. See also http://stackoverflow.com/a/2120183 point 4 – BalusC Oct 08 '15 at 11:55
  • @BalusC, I've put the `loadFermes()` method in the PostConstruct, now the `delete()` is working but I need to refresh the page manually to see changes, same thing happened to the `add()` method. So I changed the methods from void to String and returned --`return "profile.xhtml?faces-redirect=true";` now everything works, if you have any other advices about the changes that I've made please let me know, Thanks a ton! – Dwix Oct 08 '15 at 13:06

0 Answers0