我在我的JSF 2.2页面中有一个<f:metadata>
中的事件监听器,它的preRenderView不能处理每个页面调用,它只适用于第一页加载和我使用add()
方法时列表刷新时显示添加的行,但我根本无法使用delete()
方法,没有任何内容被删除,视图也不刷新。
但是如果我忽略meta tag
并且只是在getListe()
方法中加载列表,那么一切正常,但我不应该将业务代码放在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 );
}
}
..
..
..