jsfelicefaces

Access static property in JSF


I have a static List of Select Items in one of my backing beans:

private static List<SelectItem> countries = new ArrayList<SelectItem>();

with the following getters and setters:

public static List<SelectItem> getCountries()     {
    return countries;
}

public static void setCountries(List<SelectItem> countries) {
    LoadSelectItemsBean.countries = countries;
}

I am having trouble with accessing the static List through my XHTML page. The code I have tried is as follows:

<ace:simpleSelectOneMenu id="countryField"
   value="#{generalCarrierDataViewBean.carrierBean.countryId}">
   <f:selectItems value="#{loadSelectItemsBean.countries}" />
   <ace:ajax />
</ace:simpleSelectOneMenu>

The problem line is:

 <f:selectItems value="#{loadSelectItemsBean.countries}" />

The exception which results is:

javax.el.PropertyNotFoundException: /pages/GeneralCarrierData.xhtml @394,64 value="#{loadSelectItemsBean.states}": Property 'states' not found on type com.oag.reference.util.LoadSelectItemsBean

Can anbody advise on how to correctly reference a static property from a backing bean?

Thanks


Solution

  • Properties are per definition not static. So getters and setters can simply not be static, although they can in turn reference a static variable. But the outside world does not see that.

    You've 3 options:

    1. Remove the static modifier from the getter. The whole setter is unnecessary, you can just remove it.

      public List<SelectItem> getCountries()     {
          return countries;
      }
      
    2. Create an EL function if you really insist in accessing static "properties" (functions). Detail can be found in this answer: How to create a custom EL function to invoke a static method?

    3. Turn the whole List<SelectItem> thing into an enum and make use of OmniFaces <o:importConstants>. Detail can be found in this answer: How to create and use a generic bean for enums in f:selectItems?