【问题标题】:Access static property in JSF在 JSF 中访问静态属性
【发布时间】:2015-05-20 03:26:15
【问题描述】:

我的一个支持 bean 中有一个静态的选择项列表:

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

使用以下 getter 和 setter:

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

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

我无法通过我的 XHTML 页面访问静态列表。我试过的代码如下:

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

问题线是:

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

导致的异常是:

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

有人可以建议如何正确地从支持 bean 引用静态属性吗?

谢谢

【问题讨论】:

    标签: jsf el icefaces


    【解决方案1】:

    只需创建一个返回静态属性的非静态方法:

    // 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;
    }
    

    【讨论】:

      【解决方案2】:

      属性是根据定义而不是static。因此,getter 和 setter 不能是静态的,尽管它们可以反过来引用静态变量。但外界看不到这一点。

      您有 3 个选择:

      1. 从 getter 中移除 static 修饰符。整个 setter 是不必要的,你可以删除它。

        public List<SelectItem> getCountries()     {
            return countries;
        }
        
      2. 如果您确实坚持访问静态“属性”(函数),请创建一个 EL 函数。可以在这个答案中找到详细信息:How to create a custom EL function to invoke a static method?

      3. 将整个List&lt;SelectItem&gt; 变成enum 并利用OmniFaces &lt;o:importConstants&gt;。可以在这个答案中找到详细信息:How to create and use a generic bean for enums in f:selectItems?

      【讨论】:

      • 谢谢,我可能会沿着选项 1 的路线,将我的 bean 更改为我之前的配置方式。
      猜你喜欢
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      相关资源
      最近更新 更多