单选按钮选择的值未反映在命令按钮ajax调用中

时间:2012-05-22 06:41:30

标签: jsf-2

我的单选按钮有问题。我在一个字段集中有一组2个单选按钮和一个命令按钮。根据单选按钮的选择,将通过ajax调用显示所尊重的值。但是问题始终是第一个按钮是在命令按钮动作中进行查询,这就是为什么总是显示第一个值,但是第二个单选按钮被选中。这是我的代码

// test.xhtml

    <h:panelGroup id="demandDashBoardPanelGroupID">
    <fieldset> 
    <legend style="font-family:Arial;font-size: 12px;">Demand Dashboard</legend>

     <table>
        <tr>
        <td> 
            <h:selectOneRadio immediate="true" id="groupbyid" value="#{demandManagedBean.selectedRadioButton}" styleClass="labelstyle">  
                    <f:selectItem itemLabel="Group By Creation Date Time" itemValue="1" />  
                    <f:selectItem itemLabel="Group By Location" itemValue="2" />
            </h:selectOneRadio>
        </td>
        <td>

            <h:commandButton id="fetchbuttonID" value="Fetch" 
                    style="font-size:13px;width:60px;height:25px;" action="#{demandManagedBean.displayDemandRequestsTable}">
                    <f:setPropertyActionListener target="#{demandManagedBean.selectedRadioButtonParam}" value="#{demandManagedBean.selectedRadioButton}"/>
                    <f:ajax render=":parentformid:demandrequestpanelgrpid" immediate="true"/> 
            </h:commandButton>
        </td>
        </tr>
        </table>

     </fieldset>

     </h:panelGroup>

    <h:panelGroup id="demandrequestpanelgrpid" rendered="#{demandManagedBean.renderedFieldSet}"> 

     <fieldset id="demandrequestfieldsetid"> 

     <legend style="font-family:Arial;font-size: 12px;">Demand Requests</legend>

            <h:outputText rendered="#{demandManagedBean.renderCreationdateDmdtable}" value="first table"/>

            <h:outputText rendered="#{demandManagedBean.renderDmdLocationTable}" value="second table"/>

     </fieldset> 

   </h:panelGroup>

//支持bean动作

public void displayDemandRequestsTable(){


        if(selectedRadioButtonParam!= null && selectedRadioButtonParam.endsWith("1")){

            renderCreationdateDmdtable = true;
            renderDmdLocationTable = false;

        }else if(selectedRadioButtonParam!= null && selectedRadioButtonParam.endsWith("2")){

            renderCreationdateDmdtable = false;
            renderDmdLocationTable = true;

        }
        renderedFieldSet = true;
    }

1 个答案:

答案 0 :(得分:1)

此处不需要f:setPropertyActionListener将所选值输入您的支持bean。只需在execute标记中添加f:ajax属性,然后移除immediate=true

<h:commandButton ...>
   <f:ajax render=":parentformid:demandrequestpanelgrpid" execute="@form" /> 
</h:commandButton>

您也可以只提供要执行的组件的ID,而不是整个表单:

<h:commandButton ...>
   <f:ajax render=":parentformid:demandrequestpanelgrpid" execute="groupbyid" /> 
</h:commandButton>

还要确保在您的支持bean中正确初始化selectedRadioButton并且该bean至少位于@ViewScoped

更新: 如果您删除f:setPropertyActionListener,则不会设置selectedRadioButtonParam。然后,您需要在操作方法中检查selectedRadioButton