How to set Boolean value to an 'Yes' or 'No' h:selectOneRadio

前端 未结 2 778
有刺的猬
有刺的猬 2021-02-15 15:57

I have the following code in JSF-2 Mojarra 2.0.8 running on Tomcat 7




        
相关标签:
2条回答
  • 2021-02-15 16:46

    Use a simple Boolean value in your backing bean:

    private Boolean choice;
    // getter and setter
    

    And in the facelet:

    <h:selectOneRadio id="yesNoRadio" value ="#{user.choice}">
      <f:selectItem itemValue="#{false}" itemLabel="No" />
      <f:selectItem itemValue="#{true}" itemLabel="Yes"/>
      <f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
    </h:selectOneRadio>
    ..
    <h:panelGrid  id="userDetailsGrid">
      <h:panelGrid columns="2" rendered="#{user.choice}">
      ..
      </h:panelGrid>
      ..
    </h:panelGrid>
    
    0 讨论(0)
  • 2021-02-15 16:51

    You can also just check the enum value in the rendered attribute. Enums are evaluated as strings in EL, so you can do a simple string comparison. This way you can keep your original code.

    <h:panelGrid columns="2" rendered="#{user.yesNoRadio == 'Yes'}">
    

    By the way, you perhaps really want to use execute="@this" instead of execute="@form" (or just remove it altogether, it defaults to @this already). The execute="@form" will process the entire form and thus also fire validation there where you perhaps don't want.

    0 讨论(0)
提交回复
热议问题