Question

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>
Was it helpful?

Solution

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

    // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top