【问题标题】:Enum properties are not retrieved by OmniFaces in Spring / JSF projectsOmniFaces 在 Spring / JSF 项目中不检索枚举属性
【发布时间】:2014-05-07 01:51:11
【问题描述】:

鉴于以下enum

package util;

public enum IntegerConstants
{
    DATA_TABLE_PAGE_LINKS(10);

    private final int value;

    private IntegerConstants(int con) {
        this.value = con;
    }

    public int getValue() {
        return value;
    }
}

这里给出的常量应该在 XHTML 页面上检索,如下所示。

<ui:composition template="/WEB-INF/admin_template/Template.xhtml"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:o="http://omnifaces.org/ui">

    <ui:define name="title">Test</ui:define>

    <ui:define name="content">
        <h:form id="form" prependId="true">
            <o:importConstants var="const" type="util.IntegerConstants"/>
            DATA_TABLE_PAGE_LINKS : #{const.DATA_TABLE_PAGE_LINKS.value}
        </h:form>
    </ui:define>
</ui:composition>

这适用于在 GlassFish 4.0 上运行 JSF 托管 bean 的企业应用程序。

但在使用 Spring (4.0 GA)、JSF 2.2.6、PrimeFaces 5.0 final、PrimeFaces Extensions 2.0.0 final 在 Tomcat 8.0.3.0 上运行的项目中,同样的事情不起作用。

这应该与 Spring 无关。

给定的enum 位于应用程序构建文件夹下的WEB-INF/classes 文件夹中(其类文件)。

很难找出问题的实际原因,因为没有抛出错误或异常。浏览器上的页面只是空白,在服务器终端上什么也看不到。

OmniFaces 版本是 1.7。

尝试使用OmniFaces 1.8-SNAPSHOT,但问题仍然存在。


部分回答:

当我将 &lt;o:importConstants&gt;var 属性的值从 const 更改为不同的值时,这有效,如下所示。

<o:importConstants var="literal" type="util.IntegerConstants"/>
DATA_TABLE_PAGE_LINKS : #{literal.DATA_TABLE_PAGE_LINKS.value}

显然,const 的值似乎已被保留在某个地方,但这太难以置信了,因为具有 const 值的相同内容在上述另一个应用程序中也能正常工作!

【问题讨论】:

    标签: jsf tomcat el jsf-2.2 omnifaces


    【解决方案1】:

    这与 EL 的关系比与 JSF/Spring/OmniFaces 的关系更大。 Tomcat 使用的 Apache EL 实现对于保留关键字确实非常严格。例如 #{bean.class.name}(如打印 bean.getClass().getName())在 GlassFish 使用的 Oracle EL 实现中是可能的,但在 Tomcat 使用的 Apache EL 实现中则不是。您应该将其写为#{bean['class'].name}。在Java Language specificationchapter 3.9 中列出的所有其他Java 关键字,在EL specification 的第1.17 章中没有列出,被 Apache EL 实现阻止。 const 确实在其中。

    附带说明,建议常量var 以大写字母开头。此约定允许更好地区分 EL 范围内的托管 bean 实例和常量引用。它还可以立即解决您的问题,因为Constconst 不同。

    <o:importConstants var="Const" type="util.IntegerConstants" />
    DATA_TABLE_PAGE_LINKS : #{Const.DATA_TABLE_PAGE_LINKS.value}
    

    或者只是重命名枚举,var 默认为Class#getSimpleName()

    <o:importConstants type="util.Const" />
    DATA_TABLE_PAGE_LINKS : #{Const.DATA_TABLE_PAGE_LINKS.value}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 2018-02-15
      • 2011-02-16
      • 2019-06-04
      相关资源
      最近更新 更多