p:ajax调用后没有设置selectOneMenu值

时间:2022-10-08 07:36:16

Im trying to have the option set to a specific value from the backend after an AJAX-call, however although all other field-types are successfully set the selectOneMenu is not. Im using Primefaces/JSF.

我试图在AJAX调用后将选项设置为后端的特定值,但是尽管所有其他字段类型都已成功设置,但selectOneMenu不是。我使用Primefaces / JSF。

xhtml:

<p:inputText id="cpr" value="#{customerbean.customer.cpr}">
                <p:ajax event="change" listener="#{customerbean.fetchCustomerDatafromCBS}" update="@form" immediate="true" >
                    <f:param name="cprNumber" value="#{customerbean.customer.cpr}"/>
                </p:ajax>                 
                <f:validator validatorId="cprValidator" />                    
            </p:inputText>   


                <p:selectOneMenu id="gender" value="#{customerbean.customer.gender}" required="#{!customerbean.disabled}" requiredMessage="#{text['validation.error.required.gender']}" disabled="#{customerbean.disabled}">                      
                <f:selectItem itemLabel="Select One" itemValue="" noSelectionOption="true" />  
                <f:selectItem itemLabel="Male" itemValue="Male" />  
                <f:selectItem itemLabel="Female" itemValue="Female"  />  
                </p:selectOneMenu>

bean:

        getCustomer().setGender(fetchedCustomer.getGender().trim());

NOTE I just discovered that the value IS set (because the database row is inserted with the correct value) - but the selectOneMenu UI object on the browser does not reflect that.

注意我刚刚发现值已设置(因为数据库行插入的值正确) - 但浏览器上的selectOneMenu UI对象没有反映出来。

ENTIRE VIEW:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html 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"
      xmlns:pe="http://primefaces.org/ui/extensions"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:t="http://myfaces.apache.org/tomahawk">  

    <ui:composition template="commonLayout.xhtml">

        <ui:define name="content">

            <p:panelGrid columns="2" styleClass="companyHeaderGrid" >  
                <f:facet name="header">  
                    Create customer
                </f:facet>  

                <h:outputLabel for="cpr" value="CPR" />
                <h:outputLabel for="passportno" value="Passport No." />

                <p:inputText id="cpr" value="#{customerbean.customer.cpr}">
                    <p:ajax event="change" listener="#{customerbean.fetchCustomerDatafromCBS}" update="@form" immediate="true" >
                        <f:param name="cprNumber" value="#{customerbean.customer.cpr}"/>
                    </p:ajax>                 
                    <f:validator validatorId="cprValidator" />                    
                </p:inputText>   

                <p:inputText id="passportno" value="#{customerbean.customer.passportno}" readonly="#{customerbean.disabled}">                
                    <f:validator validatorId="passportnoValidator" />
                </p:inputText>                          

                <h:outputLabel for="name" value="Name"/>                
                <h:outputLabel for="nationality" value="Nationality" />

                <p:inputText id="name" value="#{customerbean.customer.name}" required="true" requiredMessage="#{text['validation.error.required.name']}" readonly="#{customerbean.disabled}"/>
                <p:inputText id="nationality" value="#{customerbean.customer.nationality}" required="true" requiredMessage="#{text['validation.error.required.nationality']}" readonly="#{customerbean.disabled}" />                                                

                <h:outputLabel for="dob" value="Date of Birth" />
                <h:outputLabel for="address" value="Address" />

                <p:calendar id="dob"  value="#{customerbean.customer.dob}" pattern="ddmmyyyy" readonly="#{customerbean.disabled}" />                
                <p:inputText id="address"  value="#{customerbean.customer.address}" readonly="#{customerbean.disabled}" />                               

                <h:outputLabel for="mailingaddress" value="Mailing Address" />
                <h:outputLabel for="gender" value="Gender" />

                <p:inputText id="mailingaddress" value="#{customerbean.customer.mailingAddress}" readonly="#{customerbean.disabled}" />                                                            

                <p:selectOneMenu id="gender" value="#{customerbean.customer.gender}" required="#{!customerbean.disabled}" requiredMessage="#{text['validation.error.required.gender']}" disabled="#{customerbean.disabled}">                      
                    <f:selectItem itemLabel="Select One" itemValue="" noSelectionOption="true" />  
                    <f:selectItem itemLabel="Male" itemValue="Male" />  
                    <f:selectItem itemLabel="Female" itemValue="Female"  />  
                </p:selectOneMenu>              

                <h:outputLabel for="mobileno" value="Mobile No."  />
                <h:outputLabel for="faxno" value="Fax No." />

                <p:inputText id="mobileno"  value="#{customerbean.customer.mobileno}" required="true" requiredMessage="#{text['validation.error.required.mobileno']}" validatorMessage="#{text['validation.error.mobileno']}" readonly="#{customerbean.disabled}" >
                    <f:validateLongRange minimum="0" maximum="99999999999999" />
                </p:inputText>
                <p:inputText id="faxno"  value="#{customerbean.customer.faxno}" readonly="#{customerbean.disabled}" />                

                <h:outputLabel for="otherno" value="Other No." />                
                <h:outputLabel for="primaryemail" value="Primary email" />              

                <p:inputText id="otherno"  value="#{customerbean.customer.otherno}" readonly="#{customerbean.disabled}" />
                <p:inputText id="primaryemail"  value="#{customerbean.customer.primaryemail}" required="false" validatorMessage="#{text['validation.error.required.email']}" readonly="#{customerbean.disabled}">                   
                    <f:validateRegex pattern="([\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z])?" />
                </p:inputText>    

                <h:outputLabel for="alternativeemail" value="Alternative email" />              
                <p:inputText id="alternativeemail"  value="#{customerbean.customer.alternativeemail}" required="false" validatorMessage="#{text['validation.error.required.email']}" readonly="#{customerbean.disabled}">                   
                    <f:validateRegex pattern="([\w\.-]*[a-zA-Z0-9_]@[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z])?" />
                </p:inputText>                    

                <h:outputLabel for="maritalstatus" value="Marital status" />
                <h:outputLabel for="nochildren" value="No. of Children" />                

                <p:selectOneMenu id="maritalstatus" value="#{customerbean.customer.maritalStatus}" disabled="#{customerbean.disabled}">  
                    <f:selectItem itemLabel="Select One" itemValue="" />  
                    <f:selectItem itemLabel="Single" itemValue="Single" />  
                    <f:selectItem itemLabel="Married" itemValue="Married"/>  
                    <f:selectItem itemLabel="Divorced" itemValue="Divorced"/>  
                    <f:selectItem itemLabel="Widow" itemValue="Widow"/>  
                </p:selectOneMenu>  

                <p:inputText id="nochildren"  value="#{customerbean.customer.nochildren}" readonly="#{customerbean.disabled}" />                

                <f:facet name="footer">  
                    <center>
                        <p:commandButton action="#{customerbean.add(customerbean.customer)}" ajax="false" value="Save" icon="ui-icon-check" />

                        <p:commandButton value="Reset" update="cpr passportno name nationality dob address mailingaddress gender genderhidden mobileno faxno otherno primaryemail alternativeemail maritalstatus maritalstatushidden nochildren messages" process="@this"  
                                         icon="ui-icon-cancel">  
                            <pe:resetInput for="cpr passportno name nationality dob address mailingaddress gender genderhidden mobileno faxno otherno primaryemail alternativeemail maritalstatus maritalstatushidden nochildren"/>  
                        </p:commandButton>              
                    </center>
                </f:facet>  
            </p:panelGrid>  
                            <p:inputText id="genderhidden" type="hidden" value="#{customerbean.customer.gender}" />
                            <p:inputText id="maritalstatushidden" type="hidden" value="#{customerbean.customer.maritalStatus}" />
        </ui:define>

    </ui:composition> 

</html>

2 个解决方案

#1


0  

Set customerbean to @ViewScoped

将customerbean设置为@ViewScoped

eg:

@ManagedBean
@ViewScoped
public class customerbean implements Serializable {

}

#2


0  

Ok, I found the culprit: noSelectionOption="true" on the f:selectItem. Once I remove that, it works fine. The reason maritalStatus worked is because it doesnt have the noSelectionOption attribute.

好的,我找到了罪魁祸首:f:selectItem上的noSelectionOption =“true”。一旦我删除它,它工作正常。 maritalStatus工作的原因是因为它没有noSelectionOption属性。

#1


0  

Set customerbean to @ViewScoped

将customerbean设置为@ViewScoped

eg:

@ManagedBean
@ViewScoped
public class customerbean implements Serializable {

}

#2


0  

Ok, I found the culprit: noSelectionOption="true" on the f:selectItem. Once I remove that, it works fine. The reason maritalStatus worked is because it doesnt have the noSelectionOption attribute.

好的,我找到了罪魁祸首:f:selectItem上的noSelectionOption =“true”。一旦我删除它,它工作正常。 maritalStatus工作的原因是因为它没有noSelectionOption属性。