PrimeFaces 6.0 - InputText Ajax事件+ CommandButton事件产生NULL Bean字段

时间:2016-11-14 19:40:03

标签: ajax jsf primefaces jsf-2

我希望在文本字段模糊时触发方法,因此我在此文本字段中添加了一个Ajax onblur事件,并且它可以正常工作。

我遇到的问题是使用commandButton提交带有NULL字段的bean。

当我摆脱inputText Ajax事件时,表单提交正常工作。

.xhtml文件: `

<h:head>
    <title>Saisie Cotisations</title>
</h:head>
<h:body>
    <h3>Saisie Cotisation</h3>
    <h:form id="formulaire">
        <h:panelGroup>
            <p:panelGrid columns="2" id="panelMenage" >
                <p:outputLabel value="N° Appartement" for="appart" />
                <p:inputNumber id="appart" value="#{saisieBean.menage.numAppart}" decimalPlaces="0" emptyValue="empty" size="3" thousandSeparator="" >
                    <p:ajax event="blur" listener="#{saisieBean.chargerMenage}" update="panelMenage panelCotisation"  />
                </p:inputNumber>
                <p:outputLabel value="Nom" for="nom" />
                <p:inputText id="nom" value="#{saisieBean.menage.nom}" disabled="#{saisieBean.lectureSeule}"  >
                </p:inputText>
                <p:outputLabel value="Prenom" for="prenom" />
                <p:inputText id="prenom" value="#{saisieBean.menage.prenom}" disabled="#{saisieBean.lectureSeule}" >
                </p:inputText>
                <p:outputLabel value="Adresse E-Mail" for="email" />
                <p:inputText id="email" value="#{saisieBean.menage.email}" disabled="#{saisieBean.lectureSeule}" >
                </p:inputText>
            </p:panelGrid>
        </h:panelGroup>
        <br />
        <h:panelGroup>
            <p:panelGrid columns="2" id="panelCotisation" >
                <p:outputLabel value="Type Cotisation" />
                <p:selectOneMenu value="#{saisieBean.menage.cotisation.id}" disabled="#{saisieBean.lectureSeule}" >
                    <f:selectItems value="#{saisieBean.typesCotisation}" var="tc" itemValue="#{tc.id}" itemLabel="#{tc.intitule}" />
                </p:selectOneMenu>
                <p:outputLabel value="Mois" />
                <p:selectOneMenu value="#{saisieBean.mois}" disabled="#{saisieBean.lectureSeule}" >
                    <f:selectItems value="#{saisieBean.listeMois}" />
                </p:selectOneMenu>
                <p:outputLabel value="Annee" />
                <p:inputNumber value="#{saisieBean.menage.cotisation.annee}" decimalPlaces="0"  thousandSeparator="" disabled="#{saisieBean.lectureSeule}" />
                <p:outputLabel value="Montant" />
                <p:inputNumber value="#{saisieBean.menage.cotisation.montant}" decimalPlaces="2" disabled="#{saisieBean.lectureSeule}" />
            </p:panelGrid>
        </h:panelGroup>
        <br />
        <p:commandButton value="Soumettre" actionListener="#{saisieBean.exec}" process="@form" update="@form" immediate="false" ></p:commandButton>
    </h:form>
</h:body>

`

The Bean:

package ma.syndic.bean;
import java.io.Serializable;
import java.text.DateFormatSymbols;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;

import ma.syndic.back.bean.Cotisation;
import ma.syndic.back.bean.Menage;
import ma.syndic.back.bean.TypeCotisation;
import ma.syndic.back.dao.bean.MenageDAO;
import ma.syndic.back.dao.bean.TypeCotisationDAO;

import org.joda.time.DateTime;
/**
 * 
 * @author  Mohamed ENNAHDI EL IDRISSI
 * @date    13/11/2016
 */
@ManagedBean
@ViewScoped
public class SaisieBean implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = -5492998931983521852L;
    private Menage menage;
    private Cotisation cotisation;
    private List<TypeCotisation> typesCotisation;
    private String[] listeMois;
    private String mois;
    private boolean lectureSeule = true;

    public SaisieBean() {
        // TODO Auto-generated constructor stub
    }

    @PostConstruct
    public void init() {
        this.menage = new Menage();
        this.cotisation = new Cotisation();
        try {
            this.typesCotisation = new TypeCotisationDAO().findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.listeMois = new DateFormatSymbols().getMonths();
        int i = new DateTime().monthOfYear().get();
        this.mois = this.listeMois[ i - 1 ];
        this.menage.getCotisation().setAnnee(new DateTime().year().get());
    }
    public void exec() {
        System.out.println("Exec fired");
        System.out.println("EMail: " + this.menage.getEmail());
    }

    public void chargerMenage() {
        System.out.println("chargerMenage fired");
        if (this.menage.getNumAppart() != null) {
            this.lectureSeule = false;
            try {
                Menage m = new MenageDAO().find(this.menage);
                if (m != null) {
                    this.menage = m;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public Menage getMenage() {
        return menage;
    }
    public void setMenage(Menage menage) {
        this.menage = menage;
    }
    public Cotisation getCotisation() {
        return cotisation;
    }
    public void setCotisation(Cotisation cotisation) {
        this.cotisation = cotisation;
    }

    public List<TypeCotisation> getTypesCotisation() {
        return typesCotisation;
    }

    public void setTypesCotisation(List<TypeCotisation> typesCotisation) {
        this.typesCotisation = typesCotisation;
    }
    public String[] getListeMois() {
        return listeMois;
    }
    public void setListeMois(String[] listeMois) {
        this.listeMois = listeMois;
    }
    public String getMois() {
        return mois;
    }
    public void setMois(String mois) {
        this.mois = mois;
    }
    public boolean isLectureSeule() {
        return lectureSeule;
    }
    public void setLectureSeule(boolean lectureSeule) {
        this.lectureSeule = lectureSeule;
    }
}

0 个答案:

没有答案