How to reference constants in EL?

tau-neutrino picture tau-neutrino · Sep 17, 2010 · Viewed 68.5k times · Source

How do you reference an constants with EL on a JSP page?

I have an interface Addresses with a constant named URL. I know I can reference it with a scriplet by going: <%=Addresses.URL%>, but how do I do this using EL?

Answer

BalusC picture BalusC · Sep 17, 2010

EL 3.0 or newer

If you're already on Java EE 7 / EL 3.0, then the @page import will also import class constants in EL scope.

<%@ page import="com.example.YourConstants" %>

This will under the covers be imported via ImportHandler#importClass() and be available as ${YourConstants.FOO}.

Note that all java.lang.* classes are already implicitly imported and available like so ${Boolean.TRUE} and ${Integer.MAX_VALUE}. This only requires a more recent Java EE 7 container server as early versions had bugs in this. E.g. GlassFish 4.0 and Tomcat 8.0.0-1x fails, but GlassFish 4.1+ and Tomcat 8.0.2x+ works. And you need to make absolutely sure that your web.xml is declared conform the latest servlet version supported by the server. Thus with a web.xml which is declared conform Servlet 2.5 or older, none of the Servlet 3.0+ features will work.

Also note that this facility is only available in JSP and not in Facelets. In case of JSF+Facelets, your best bet is using OmniFaces <o:importConstants> as below:

<o:importConstants type="com.example.YourConstants" />

Or adding an EL context listener which calls ImportHandler#importClass() as below:

@ManagedBean(eager=true)
@ApplicationScoped
public class Config {

    @PostConstruct
    public void init() {
        FacesContext.getCurrentInstance().getApplication().addELContextListener(new ELContextListener() {
            @Override
            public void contextCreated(ELContextEvent event) {
                event.getELContext().getImportHandler().importClass("com.example.YourConstants");
            }
        });
    }

}

EL 2.2 or older

This is not possible in EL 2.2 and older. There are several alternatives:

  1. Put them in a Map<String, Object> which you put in the application scope. In EL, map values are accessible the usual Javabean way by ${map.key} or ${map['key.with.dots']}.

  2. Use <un:useConstants> of the Unstandard taglib (maven2 repo here):

    <%@ taglib uri="http://jakarta.apache.org/taglibs/unstandard-1.0" prefix="un" %>
    <un:useConstants className="com.example.YourConstants" var="constants" />
    

    This way they are accessible the usual Javabean way by ${constants.FOO}.

  3. Use Javaranch's CCC <ccc:constantsMap> as desribed somewhere at the bottom of this article.

    <%@ taglib uri="http://bibeault.org/tld/ccc" prefix="ccc" %>
    <ccc:constantsMap className="com.example.YourConstants" var="constants" />
    

    This way they are accessible the usual Javabean way by ${constants.FOO} as well.

  4. If you're using JSF2, then you could use <o:importConstants> of OmniFaces.

    <html ... xmlns:o="http://omnifaces.org/ui">
    <o:importConstants type="com.example.YourConstants" />
    

    This way they are accessible the usual Javabean way by #{YourConstants.FOO} as well.

  5. Create a wrapper class which returns them through Javabean-style getter methods.

  6. Create a custom EL resolver which first scans the presence of a constant and if absent, then delegate to the default resolver, otherwise returns the constant value instead.