Access static property in JSF
Asked Answered
B

2

7

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

Bolme answered 17/3, 2015 at 15:44 Comment(0)
B
15

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?

Blueness answered 17/3, 2015 at 15:48 Comment(1)
Thank you, I will probably go down the route of option 1 and change my bean to how I previously had it configured.Bolme
T
0

Just create a non-static method that returns the static property:

// here you have a static String
private static String static_str;

public static String getStatic_str() {
    return static_str;
}

// in jsf page: #{myClass.str}
public String getStr() {
    return static_str;
}
Trogon answered 29/6, 2021 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.