Jsf 2 从selectOneMenu在ajax调用中呈现inputtext字段

Jsf 2 从selectOneMenu在ajax调用中呈现inputtext字段,jsf-2,Jsf 2,当我从h:SelectOne菜单更改选择时,它会正确地在h:inputtext上呈现输出。 当我没有选择任何内容时会出现问题,它仍然显示上次选择, 当选择“选择国家”时,我不想在inputtext上显示任何内容 <h:selectOneMenu id="country_id" value="#{countryBean.countryCode}" > <f:validator for="country_id" validatorId="countr

当我从h:SelectOne菜单更改选择时,它会正确地在h:inputtext上呈现输出。 当我没有选择任何内容时会出现问题,它仍然显示上次选择, 当选择“选择国家”时,我不想在inputtext上显示任何内容

<h:selectOneMenu id="country_id" value="#{countryBean.countryCode}" >
                <f:validator for="country_id" validatorId="countryNameValidator"/>
                <f:selectItem itemLabel="Select Country" itemValue=" "/>
                <f:selectItems value="#{countryBean.countryList}" var="countryDTO" itemLabel="#{countryDTO.countryName}(#{countryDTO.countryCode})" itemValue="#{countryDTO.countryCode}"/>
                <f:ajax event="change" render="countryCodeValue" execute="@this" listener="#{countryBean.setCountryCodeData}"></f:ajax>
            </h:selectOneMenu>
<h:inputText disabled="true" value="#{countryBean.countryCode}" id="countryCodeValue" required="true" >
                            </h:inputText>

您的
countryNameValidator
是罪魁祸首。如果在提交表单期间验证失败,则不会更新模型值。这完全解释了为什么您一直成功地看到最后一个选定值。如果您将
添加到表单中,并将其
id
添加到
中,那么您应该看到验证错误消息

现在,我检查了你的验证器。这实际上毫无意义。它所做的似乎与JSF builtin
required=“true”
验证器所做的完全相同。也许您创建它是为了防止空白空间值“请选择”提交。这真的毫无意义。只需使用
null
或空字符串作为“请选择”值,并坚持使用
required=“true”
。然后您可以完全删除该验证器

但是,这并不能解决最初的问题:更改下拉列表并更新关联的文本元素时出现验证错误。如果您通过
required=“true”
更改了验证器,那么这实际上更容易解决。仅当按下“真正提交”按钮时,才让
required
属性计算
true
。以下是一个启动示例:

<h:selectOneMenu ... required="#{not empty param[submit.clientId]}">
    <f:selectItem itemLabel="Select Country" itemValue="#{null}" />
    <f:selectItems ... />
    <f:ajax listener="#{countryBean.setCountryCodeData}" render="countryCodeValue" />
</h:selectOneMenu>
<h:inputText id="countryCodeValue" ... />
...
<h:commandButton binding="#{submit}" ... />

...

(注意
event
execute
属性已经有了正确的默认值;我省略了它们)

发布countryNameValidator的代码。发布的验证文件countryNameValidator使用
countryNameValidator
的确切目的是什么?
<h:selectOneMenu ... required="#{not empty param[submit.clientId]}">
    <f:selectItem itemLabel="Select Country" itemValue="#{null}" />
    <f:selectItems ... />
    <f:ajax listener="#{countryBean.setCountryCodeData}" render="countryCodeValue" />
</h:selectOneMenu>
<h:inputText id="countryCodeValue" ... />
...
<h:commandButton binding="#{submit}" ... />