0

我试图在 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>

豆:

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

注意 我刚刚发现该值已设置(因为数据库行插入了正确的值) - 但浏览器上的 selectOneMenu UI 对象并未反映这一点。

整体视图:

<?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>
4

2 回答 2

0

好的,我在 f:selectItem 上找到了罪魁祸首:noSelectionOption="true"。一旦我删除它,它就可以正常工作。maritalStatus 起作用的原因是它没有 noSelectionOption 属性。

于 2012-09-26T06:20:38.273 回答
0

将 customerbean 设置为@ViewScoped

例如:

@ManagedBean
@ViewScoped
public class customerbean implements Serializable {

}
于 2012-09-23T07:00:14.100 回答