开发者

Render SelectItem based on Property File

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 conf开发者_如何学Pythonig/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>


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")));
    }

    // ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜