Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access static property in JSF

Tags:

jsf

el

icefaces

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

like image 457
LiamWilson94 Avatar asked Mar 17 '15 15:03

LiamWilson94


1 Answers

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?

like image 166
BalusC Avatar answered Nov 08 '22 19:11

BalusC