我开始学习JSF和primeface。我有一个xhtml页面和一个Dialog但是CommandButton中的actionListener在我更新时不起作用。 请帮我。 这是我的xhtml文件的内容
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
template="/WEB-INF/template/template.xhtml">
<ui:define name="title">Continents</ui:define>
<ui:define name="content">
<h:head >
<f:loadBundle basename="fst.info.memoire.bundle.messages" var="msg"/>
</h:head>
<h:body>
<div class="ui-g">
<div class="ui-g-12">
<div class="card">
<h1>Gestion des continents</h1>
<h:form id="main" >
<p:growl id="messages" showDetail="true" />
<h:panelGrid columns="1" style="margin-bottom:10px" cellpadding="5">
<p:toolbar>
<f:facet name="left">
<p:commandButton value="Nouveau" icon="fa fa-plus" onclick="PF('continentAdd').show();" update=":add:displaye1" />
</f:facet>
</p:toolbar>
<p:dataTable id="listTab" var="continent" value="#{continentBean.listerContinent()}" selectionMode="single"
rowKey="#{continent.code}" reflow="true" selection="#{continentBean.selectedContinent}" emptyMessage="Aucun enregistrement">
<f:facet name="header">Liste des continents</f:facet>
<p:column headerText="Code">
<h:outputText value="#{continent.code}" />
</p:column>
<p:column headerText="Nom" >
<h:outputText value="#{continent.nom}" />
</p:column>
<p:column headerText="Actions" >
<div style="text-align: center;">
<p:commandButton id="modifier" icon="fa fa-pencil" title="Modifier" />
<p:commandButton id="supprimer" icon="fa fa-trash" title="Supprimer" actionListener="#{continentBean.supprimerContinent}" update=":main:listTab,:main:messages ">
<p:confirm header="Confirmation" message="Etes vous sur de vouloir supprimer cet enregistrement?" icon="fa fa-exclamation-triangle" />
</p:commandButton>
</div>
</p:column>
</p:dataTable>
</h:panelGrid>
<p:confirmDialog global="true" showEffect="fade" >
<p:commandButton value="Oui" type="button" styleClass="ui-confirmdialog-yes" icon="fa fa-check" />
<p:commandButton value="Non" type="button" styleClass="ui-confirmdialog-no" icon="fa fa-close" />
</p:confirmDialog>
</h:form>
<p:dialog header="Ajouter un continent" widgetVar="continentAdd"
resizable="false" modal="true" id="addCont" >
<h:form id="add">
<p:growl id="growl" showDetail="true" />
<h:panelGrid id="displaye1" columns="3" cellpadding="4"
style="margin:0 auto;">
<p:outputLabel for="code" value="Code" />
<p:inputText value="#{continentBean.code}" id="code" required="true" requiredMessage="#{msg['continent.code']}">
<f:ajax event="blur" render="messagecode"/>
</p:inputText>
<p:message for="code" style="color:red" id="messagecode"/>
<p:outputLabel for="nom" value="Nom" />
<p:inputText value="#{continentBean.nom}" id="nom" required="true" requiredMessage="#{msg['continent.nom']}">
<f:ajax event="blur" render="messageNom"/>
</p:inputText>
<p:message for="nom" style="color:red" id="messageNom"/>
</h:panelGrid>
<p:separator />
<div align="center">
<p:commandButton value="Ajouter" update=":main:listTab,:main:messages " icon="fa fa-check" ajax="true"
action="#{continentBean.ajouterContinent()}"
oncomplete="PF('continentAdd').hide();" />
<p:commandButton value="Annuler" icon="fa fa-close"
oncomplete="PF('continentAdd').hide();" />
</div>
</h:form>
</p:dialog>
</div>
</div>
</div>
</h:body>
</ui:define>
</ui:composition>
这是我的backingBean
package fst.info.memoire.presentation.admin;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import fst.info.memoire.model.Continent;
import fst.info.memoire.service.admin.interf.ContinentService;
@Component
@ManagedBean(name="continentBean")
@SessionScoped
public class ContinentBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@ManagedProperty(value="#{continentService}")
@Autowired
ContinentService continentService;
String code ;
String nom ;
Continent selectedContinent ;
List<Continent> listContinent ;
public ContinentBean() {
super();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public ContinentService getContinentService() {
return continentService;
}
public void setContinentService(ContinentService continentService) {
this.continentService = continentService;
}
public Continent getSelectedContinent() {
return selectedContinent;
}
public void setSelectedContinent(Continent selectedContinent) {
this.selectedContinent = selectedContinent;
}
public List<Continent> getListContinent() {
return listContinent;
}
public void setListContinent(List<Continent> listContinent) {
this.listContinent = listContinent;
}
public List<Continent> listerContinent() {
return continentService.listContinent();
}
public void ajouterContinent(ActionEvent event){
System.out.println("---------Click!");
FacesContext context = FacesContext.getCurrentInstance();
try{
Continent continent = new Continent();
continent.setCode(this.code);
continent.setNom(this.nom);
continentService.ajouterContinent(continent);
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Succes", "Continent ajouté avec succès");
context.addMessage(null, message);
}catch(Exception e){
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erreur", "Erreur lors de l'ajout");
context.addMessage(null, message);
}
}
public void supprimerContinent(){
addMessage("System Error", "Please try again later.");
}
public void addMessage(String summary, String detail) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail);
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
答案 0 :(得分:0)
您的命令按钮应如下所示:
<p:commandButton value="Ajouter" update=":main:listTab,:main:messages " icon="fa fa-check"
actionListener="#{continentBean.ajouterContinent()}"
oncomplete="PF('continentAdd').hide();" />
您不需要ajax =&#34; true&#34;,这是默认设置,最重要的是,当您打算进行ajax调用时,您必须使用actionListener
而不是{{1} }(用于标准的同步POST请求)。