p:ajax更新不能与p:selectBooleanCheckbox一起使用

Cen*_*321 4 ajax jsf primefaces

我正在使用JSF 2和Primefaces 4并且有以下问题:

我的XHTML中有以下代码:

<h:form id="form">
<table>  
    <tr id="formats">
        <td>Formats</td>
        <td>
            <p:selectBooleanCheckbox value="#{bean.entity.formatted}">
                <p:ajax event="change" update="formatsSelect" /> 
            </p:selectBooleanCheckbox>
            <p:selectOneMenu id="formatsSelect" rendered="#{bean.entity.formatted}">
                <f:selectItems value="#{bean.formats}" />
            </p:selectOneMenu> 
        </td>
    </tr>
</table>
</h:form>
Run Code Online (Sandbox Code Playgroud)

它输出一个复选框和一个选择菜单,我期望的是,当我选中复选框时,选择菜单应该出现,并且当我取消选中它时应该消失....但没有任何反应,我检查并取消选中它,选择菜单不受影响.

理论上这应该有效,因为selectBooleanCheckbox值绑定到entity.formatted布尔值,selectOneMenu中的呈现值绑定到entity.formatted值,p:ajax指向update属性和事件中的正确id是正确的.我知道的最后一点,因为我在我的bean中创建了一个监听器,它打印了格式化的值:

public void changeListener(){
    System.out.println(this.entity.isFormatted());
}
Run Code Online (Sandbox Code Playgroud)

并将p:ajax更改为:

<p:ajax event="change" update="formatsSelect" listener="#{bean.changeListener}" /> 
Run Code Online (Sandbox Code Playgroud)

它打印出控制台中格式化的值.我究竟做错了什么?

Kis*_*ash 13

由于您rendered在component(p:selectOneMenu id="formatsSelect")上使用并更新了相同的组件,因此无法工作.

因为在更新时可能尚未将该组件添加到/存在于组件树中.

所以使用h:panelGroup它周围和更新,并使用renderedp:selectOneMenu.

<p:selectBooleanCheckbox value="#{bean.entity.formatted}">
      <p:ajax event="change" update="formatsSelectPG" /> 
</p:selectBooleanCheckbox>

<h:panelGroup id="formatsSelectPG">
      <p:selectOneMenu id="formatsSelect" rendered="#{bean.entity.formatted}">
            <f:selectItems value="#{bean.formats}" />
       </p:selectOneMenu> 
</h:panelGroup> 
Run Code Online (Sandbox Code Playgroud)