如何将布尔值设置为“Yes”或“否” h:选择一个无线电

发布于 2025-01-04 05:15:06 字数 2039 浏览 1 评论 0原文

我在 Tomcat 7 上运行的 JSF-2 Mojarra 2.0.8 中有以下代码

<h:panelGrid  id="yesNoRadioGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadioGridFlag}">
    <h:outputText id ="otherLbl" value="Select Yes or No"></h:outputText>
    <h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
        <f:selectItems value="#{user.choices}"/>                    
        <f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
    </h:selectOneRadio>
</h:panelGrid>
</h:panelGrid>      
<h:message for ="yesNoRadio"> </h:message>
<h:panelGrid  id="userDetailsGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadio}">
    <h:outputLabel>Name :</h:outputLabel>
    <h:inputText id="customerName" value="#{user.customerName}"></h:inputText>
    <h:outputLabel>Salary: </h:outputLabel>
    <h:inputText id="customerSalary" value="#{user.customerSalary}"></h:inputText>
</h:panelGrid>
</h:panelGrid>

我的 ManagedBean 包含以下内容

private enum Choice {

    Yes, No; 
    } 
private Choice yesNoRadio;
public Choice[] getChoices() {
    return Choice.values(); 
    }
public Choice getYesNoRadio() {
    return yesNoRadio;
}
public void setYesNoRadio(Choice yesNoRadio) {
    this.yesNoRadio = yesNoRadio;
}

如何根据在中选择的布尔值 (user.yesNoRadio) 呈现我的“userDetailsGrid”

我通过在我的中添加以下内容找到了一个解决方法mangedbean

private Boolean yesNoRadio;
public SelectItem[] getMyBooleanValues() {   
    return new SelectItem[] {    
    new SelectItem(Boolean.TRUE, "Yes"),   
    new SelectItem(Boolean.FALSE, "No")    
    };   
}  

并改变我的观点

<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
    <f:selectItems value="#{user.myBooleanValues}" />                   
    <f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>

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

<h:panelGrid  id="yesNoRadioGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadioGridFlag}">
    <h:outputText id ="otherLbl" value="Select Yes or No"></h:outputText>
    <h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
        <f:selectItems value="#{user.choices}"/>                    
        <f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
    </h:selectOneRadio>
</h:panelGrid>
</h:panelGrid>      
<h:message for ="yesNoRadio"> </h:message>
<h:panelGrid  id="userDetailsGrid">
<h:panelGrid columns="2" rendered="#{user.yesNoRadio}">
    <h:outputLabel>Name :</h:outputLabel>
    <h:inputText id="customerName" value="#{user.customerName}"></h:inputText>
    <h:outputLabel>Salary: </h:outputLabel>
    <h:inputText id="customerSalary" value="#{user.customerSalary}"></h:inputText>
</h:panelGrid>
</h:panelGrid>

My managedBean contain following

private enum Choice {

    Yes, No; 
    } 
private Choice yesNoRadio;
public Choice[] getChoices() {
    return Choice.values(); 
    }
public Choice getYesNoRadio() {
    return yesNoRadio;
}
public void setYesNoRadio(Choice yesNoRadio) {
    this.yesNoRadio = yesNoRadio;
}

How can I render my 'userDetailsGrid' based on the boolean value (user.yesNoRadio) selected in

I found a workarround by adding following in my mangedbean

private Boolean yesNoRadio;
public SelectItem[] getMyBooleanValues() {   
    return new SelectItem[] {    
    new SelectItem(Boolean.TRUE, "Yes"),   
    new SelectItem(Boolean.FALSE, "No")    
    };   
}  

and changing my view to

<h:selectOneRadio id="yesNoRadio" value ="#{user.yesNoRadio}">
    <f:selectItems value="#{user.myBooleanValues}" />                   
    <f:ajax execute="@form" render="userDetailsGrid "></f:ajax>
</h:selectOneRadio>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一腔孤↑勇 2025-01-11 05:15:06

在支持 bean 中使用简单的布尔值:

private Boolean choice;
// getter and setter

在 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>

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>
油饼 2025-01-11 05:15:06

您还可以仅检查 rendered 属性中的枚举值。枚举在 EL 中被计算为字符串,因此您可以进行简单的字符串比较。这样你就可以保留原来的代码。

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

顺便说一句,您可能真的想使用 execute="@this" 而不是 execute="@form" (或者干脆将其完全删除,它默认为 @this 已经)。 execute="@form" 将处理整个表单,因此也会在您可能不想要的地方触发验证。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文