动作清空了selectonemenu

时间:2016-08-11 12:05:37

标签: jsf primefaces

我有一个简单的表单,有一些输入和一个selectonemenu:

            <h:panelGroup id="Client">
            <h:form id="formClient">
                <p:panelGrid columns="3" layout="grid"
                    columnClasses="labelWidth, ui-grid-col-3">
                    <p:outputLabel value="CIN:" for="txtCin" />
                    <h:panelGrid columns="2">
                        <p:inputText value="#{clientBean.client.identifiant}" id="txtCin"></p:inputText>
                        <p:commandButton value="Search" process="@parent"
                            styleClass="orangeButton" update="formClient"
                            style="margin-top: -10px;" action="#{clientBean.rechercher()}"></p:commandButton>
                    </h:panelGrid>
                    <p:message for="txtCin" />

                    <p:outputLabel value="Nom:" for="txtNom" />
                    <p:inputText id="txtNom" styleClass="form-control"
                        value="#{clientBean.client.nomClient}" required="true"
                        requiredMessage="Le nom est obligatoire" />
                    <p:message for="txtNom" />

                    <p:outputLabel value="Type de voie:" for="txtVoie" />
                    <p:selectOneMenu styleClass="form-control" 
                        value="#{clientBean.client.typeVoie}" id="txtVoie">
                        <f:selectItem itemLabel="Selectionnez voie..." itemValue=""  />
                        <f:selectItems value="#{clientBean.typeVoies}" var="typeVoie" itemLabel="#{typeVoie.libelle}" itemValue="#{typeVoie}" />
                    </p:selectOneMenu>
                    <p:message for="txtVoie" />

                    <p:outputPanel></p:outputPanel>
                    <p:outputPanel>
                        <p:commandButton value="Annuler" update="formClient"
                            styleClass="redButton" process="@this"
                            actionListener="#{clientBean.cancel()}" />
                        <p:commandButton ajax="false" value="Suivant"
                            styleClass="greenButton" action="Devis?faces-redirect=true"></p:commandButton>
                    </p:outputPanel>
                </p:panelGrid>
            </h:form>
        </h:panelGroup>

如您所见,我有一些必填字段,以及取消(清空)表单的按钮:

@ManagedBean(name = "clientBean")
@SessionScoped
public class ClientBean implements Serializable {

    @EJB
    private IClientDAO clientDAO;
    @EJB
    private ITypeVoieDAO typeVoieDAO; 

    private List<TypeVoie> typeVoies;

    private static final long serialVersionUID = -3324864097586374969L;

    private Client client = new Client();

    @PostConstruct
    public void init(){
        typeVoies = typeVoieDAO.findAll(); 
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public void rechercher(){
        if(client != null && client.getIdentifiant() != null){
            System.out.println(client.getIdentifiant());
            client = clientDAO.findByIdentifiant(client.getIdentifiant());
            if(client == null){
                cancel();
            }
        }
    }

    public void cancel(){
        client = new Client();
        System.out.println(typeVoies);
    }

    public String navigateToClientPage(){
        rechercher(); 
        return "client?faces-redirect=true";
    }

    public List<TypeVoie> getTypeVoies() {
        return typeVoies;
    }

    public void setTypeVoies(List<TypeVoie> typeVoies) {
        this.typeVoies = typeVoies;
    }
}

在页面加载时,selectonemenu加载完美,但是当我点击取消时,selectonemenu会被清空,即使我正在搜索&#34;对于客户端,并更新表单,所有其他字段都正确填充,但selectonemenu完全为空。

使用JSF 2.1和primefaces 6.0

更新: 添加了转换器,但没有任何改变......

1 个答案:

答案 0 :(得分:0)

确保在备用bean的某个位置不要清空typeVoies列表。如果你可以分享你的完整java类,你可能会获得更准确的帮助。