根据属性文件渲染SelectItem

发布于 2024-10-23 16:06:00 字数 763 浏览 5 评论 0原文

我有一个 selectOneRadio 菜单,其中包含一些 selectItem。我想显示基于属性文件的选择。例如,如果商店没有信用卡读卡器,那么我不会显示信用选项。应该有一个配置/属性文件指定显示什么和不显示什么。

有办法实现吗?我假设我需要将属性文件读入支持 bean,然后拥有类似“渲染”属性的内容。但是,我刚刚尝试过,“渲染”似乎不适用于 selectItem。

<h:selectOneRadio id="selectedPaymentMethod" layout="pageDirection" 
        value="#{selectPaymentMethodAction.selectedPaymentMethod}">

    <f:selectItem itemValue="online" itemLabel="#{paymentMsg['payment.online.lbl']}"/>
    <f:selectItem itemValue="cash" itemLabel="#{paymentMsg['payment.cash.lbl']}"/>
    <f:selectItem itemValue="credit" itemLabel="#{paymentMsg['payment.credit.lbl']}"/>
    <f:selectItem itemValue="debit" itemLabel="#{paymentMsg['payment.debit.lbl']}"/>

</h:selectOneRadio>

I have a selectOneRadio menu with some selectItem's in them. I want to show the choices based on a property file. For example, if a store doesn't have a credit card reader, then I wouldn't show the credit option. There should be a config/properties file specifiying what is shown and what is not.

Is there a way to accomplish that? I assume I need to read the properties file into the backing bean and then have something like a 'rendered' attribute. However, I just tried and 'rendered' doesn't seem to work for selectItem.

<h:selectOneRadio id="selectedPaymentMethod" layout="pageDirection" 
        value="#{selectPaymentMethodAction.selectedPaymentMethod}">

    <f:selectItem itemValue="online" itemLabel="#{paymentMsg['payment.online.lbl']}"/>
    <f:selectItem itemValue="cash" itemLabel="#{paymentMsg['payment.cash.lbl']}"/>
    <f:selectItem itemValue="credit" itemLabel="#{paymentMsg['payment.credit.lbl']}"/>
    <f:selectItem itemValue="debit" itemLabel="#{paymentMsg['payment.debit.lbl']}"/>

</h:selectOneRadio>

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

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

发布评论

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

评论(1

枕头说它不想醒 2024-10-30 16:06:00

使用 ,您可以根据捆绑文件使用 List 提供该内容。这样您就可以使用常用的 Java 代码来控制是否应添加该项目。

例如

<f:selectItems value="#{selectPaymentMethodAction.paymentMethods}" />

private List<SelectItem> paymentMethods; // +getter

public Bean() {
    paymentMethods = new ArrayList<SelectItem>();
    ResourceBundle bundle = ResourceBundle.getBundle("com.example.Messages", FacesContext.getCurrentInstance().getViewRoot().getLocale());

    if (condition) {
        paymentMethods.add(new SelectItem("online", bundle.getString("payment.online.lbl")));
    }

    // ...
}

Use <f:selectItems> which you feed with a List<SelectItem> based on the bundle file. This way you can control using usual Java code whether the item should be added or not.

E.g.

<f:selectItems value="#{selectPaymentMethodAction.paymentMethods}" />

with

private List<SelectItem> paymentMethods; // +getter

public Bean() {
    paymentMethods = new ArrayList<SelectItem>();
    ResourceBundle bundle = ResourceBundle.getBundle("com.example.Messages", FacesContext.getCurrentInstance().getViewRoot().getLocale());

    if (condition) {
        paymentMethods.add(new SelectItem("online", bundle.getString("payment.online.lbl")));
    }

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