Question

Given the following 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;
    }
}

The constant given here should be retrieved on an XHTML page like as follows.

<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>

This works in an enterprise application having JSF managed beans running on GlassFish 4.0.

This same thing, however does not work in a project that uses Spring (4.0 GA), JSF 2.2.6, PrimeFaces 5.0 final, PrimeFaces Extensions 2.0.0 final running on Tomcat 8.0.3.0.

This should not be related to Spring.

The enum given is available (its class file) in the WEB-INF/classes folder under the application's build folder.

It is difficult to find out the actual cause of the problem because no error or exception is thrown. The page on the browser is just left blank and there is nothing to see on the server terminal.

The OmniFaces version is 1.7.

Given it a try on OmniFaces 1.8-SNAPSHOT but the problem remained stationary.


Partial answer :

This worked, when I changed the value of the var attribute of <o:importConstants> from const to something different like as follows.

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

Apparently, the value const appears to have been reserved somewhere but that's too difficult to believe because the same thing with the value const works fine in another application as mentioned above!

Was it helpful?

Solution

This is more related to EL than to JSF/Spring/OmniFaces. The Apache EL implementation as used by Tomcat is indeed quite restrictive as to reserved keywords. So is for example #{bean.class.name} (as in, print bean.getClass().getName()) possible in Oracle EL implementation as used by GlassFish, but not in Apache EL implementation as used by Tomcat. You should instead be writing it as #{bean['class'].name}. All other Java keywords listed in chapter 3.9 of Java Language specification which are not listed in chapter 1.17 of EL specification are also blocked by the Apache EL implementation. The const is indeed among them.

On a side note, it's recommended to start the constants var with an uppercase. This convention allows better distinguishing between managed bean instances and constant references in the EL scope. It also instantly solves your problem as Const is not the same as const.

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

Or just rename the enum, the var defaults namely to Class#getSimpleName().

<o:importConstants type="util.Const" />
DATA_TABLE_PAGE_LINKS : #{Const.DATA_TABLE_PAGE_LINKS.value}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top