Ajax渲染无法在SelectBooleanCheckbox上运行

时间:2014-08-28 14:27:10

标签: ajax jsf jsf-2 selectbooleancheckbox

我有一个JSF表单,我有一个selectBooleanCheckbox。如果复选框的值为true,我想在其旁边显示组合框。但不知怎的,它不起作用。

这是我的代码:

      <h:outputText value="Condition"/>
      <h:selectBooleanCheckbox id="provisioningTargetCollector" 
                           value="#{targetSource.provisioningTargetCollector}"
                           disabled="#{!sp:hasRight(facesContext, 'ManageApplication')}"
                           readonly="#{!sp:hasRight(facesContext, 'ManageApplication')}">
                           <f:ajax  render="targetCollectorsGroup" />
                           </h:selectBooleanCheckbox>

      <h:outputText value="Provisioning Target Collector"/>
      <h:panelGroup id="targetCollectorsGroup">
        <a4j:outputPanel id="targetCollectors">
          <h:selectOneMenu id="collector"
                           value="#{targetSource.selectedTargetCollector}"
                           rendered="#{empty targetSource.object.id}"
                           disabled="#{!sp:hasRight(facesContext, 'ManageApplication')}"
                           readonly="#{!sp:hasRight(facesContext, 'ManageApplication')}">
            <f:selectItem itemValue="" itemLabel="#{msgs.select_provisioning_target_collector}"/>
            <f:selectItems value="#{targetSource.targetCollectors}"/>
          </h:selectOneMenu>
          <h:selectOneMenu id="fixedTargetCollector" value="#{empty targetSource.selectedTargetCollector ? 'None' : targetSource.selectedTargetCollector}"
                           rendered="#{not empty targetSource.object.id}"
                           disabled="true"
                           readonly="true">
            <f:selectItem itemValue="#{empty targetSource.selectedTargetCollector ? 'None' : targetSource.selectedTargetCollector}"
                          itemLabel="#{empty targetSource.selectedTargetCollector ? msgs.none : targetSource.selectedTargetCollector}"/>
          </h:selectOneMenu>
        </a4j:outputPanel>
      </h:panelGroup>

Bean类方法:

private boolean _provisioningTargetCollector;

public boolean isProvisioningTargetCollector() {

    return _provisioningTargetCollector;
 }

 public void setProvisioningTargetCollector(boolean provisioningTargetCollector) {

     _provisioningTargetCollector = provisioningTargetCollector;
 }

注意:从FireBug复制

这是POST请求:

AJAX:EVENTS_COUNT   1
editForm    editForm
editForm:collector  
editForm:collectorType  
editForm:dsCorrelationRul...    
editForm:dsCreationRule 
editForm:dsDescription  
editForm:dsName 
editForm:id 2c90950048166abc0148177755040025
editForm:selectedTargetsF...    
editForm:type   Active Directory - Direct
javax.faces.ViewState   1920301575480395177:8510885827764100150
javax.faces.behavior.even...    click
javax.faces.partial.ajax    true
javax.faces.partial.event   click
javax.faces.partial.execu...    editForm:provisioningTargetCollector editForm:provisioningTargetCollector
javax.faces.partial.rende...    editForm:targetCollectorsGroup
javax.faces.source  editForm:provisioningTargetCollector
rfExt   null

响应:

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="editForm:targetCollectorsGroup"><![CDATA[<span id="editForm:targetCollectorsGroup"><span id="editForm:targetCollectors"><select id="editForm:collector" name="editForm:collector" size="1">  <option value="">Select a Provisioning Target Collector ...</option>
    <option value="AD Test">AD Test</option>
    <option value="ADWindow">ADWindow</option>
    <option value="ADTestSth">ADTestSth</option>
</select></span></span>]]></update><update id="javax.faces.ViewState"><![CDATA[1920301575480395177:8510885827764100150]]></update><extension id="org.richfaces.extension"><render>editForm:targetCollectorsGroup</render></extension></changes></partial-response>

1 个答案:

答案 0 :(得分:3)

实现所需功能的简单方法是,根据复选框的选择,重新渲染组合框。这是通过以下方式完成的:

<h:selectBooleanCheckbox binding="#{checkbox}">
    <f:ajax render="group" />
</h:selectBooleanCheckbox>
<h:panelGroup id="group">
    <h:selectOneMenu ... rendered="#{checkbox.value}" />
</h:panelGroup>

请注意,代码不需要bean中的任何特殊字段,UIComponent绑定到视图。

当然,如果您在模型中需要它,也可以通过bean属性来实现:

<h:selectBooleanCheckbox value="#{bean.value}">
    <f:ajax render="group" />
</h:selectBooleanCheckbox>
<h:panelGroup id="group">
    <h:selectOneMenu ... rendered="#{bean.value}" />
</h:panelGroup>

另外值得注意的是,通过在displaynone之间切换元素的block属性,也可以通过纯JavaScript实现此功能。但请注意,您必须保持JSF组件的状态在回发中保持一致,以防止可能的恶意攻击威胁。