【问题标题】:Render SelectItem based on Property File基于属性文件渲染 SelectItem
【发布时间】:2011-07-17 00:17:16
【问题描述】:

我有一个 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>

【问题讨论】:

    标签: java jsf render selectonemenu properties-file


    【解决方案1】:

    使用基于捆绑文件的List&lt;SelectItem&gt; 提供的&lt;f:selectItems&gt;。这样您就可以使用普通的 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")));
        }
    
        // ...
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-09
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多