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?
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?
You usually place these kinds of constants in a
Configuration
object (which has getters and setters) in the servlet context, and access them with${applicationScope.config.url}
The following does not apply to EL in general, but instead to SpEL (Spring EL) only (tested with 3.2.2.RELEASE on Tomcat 7). I think it is worth mentioning it here in case someone searches for JSP and EL (but uses JSP with Spring).
There is a workaround that is not exactly what you want, but lets you active almost the same with touching scriptlets in a quite minimal way. You can use scriptlet to put value into a JSTL variable and use clean JSTL code later in the page.
Yes, you can. You need a custom tag (if you can't find it somewhere else). I've done this:
and the tag is called:
All public static final variables will be put into a Map indexed by their Java name, so if
then the tag will wrap this in an Integer and you can reference it in a JSP:
and you don't have to write getters!
I'm defining a constant in my jsp right at the beginning:
I include the core taglib in my JSP:
Then, I make the constant available to EL by following statement:
Now, I can use it later. Here an example, where the value is just written as HTML comment for debugging purposes:
With your constant class, you can just import your class and assign the constants to local variables. I know that my answer is a sort of quick hack, but the question also bumps up when one wants to define constants directly in the JSP.
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.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 yourweb.xml
is declared conform the latest servlet version supported by the server. Thus with aweb.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:Or adding an EL context listener which calls
ImportHandler#importClass()
as below:EL 2.2 or older
This is not possible in EL 2.2 and older. There are several alternatives:
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']}
.Use
<un:useConstants>
of the Unstandard taglib (maven2 repo here):This way they are accessible the usual Javabean way by
${constants.FOO}
.Use Javaranch's CCC
<ccc:constantsMap>
as desribed somewhere at the bottom of this article.This way they are accessible the usual Javabean way by
${constants.FOO}
as well.If you're using JSF2, then you could use
<o:importConstants>
of OmniFaces.This way they are accessible the usual Javabean way by
#{YourConstants.FOO}
as well.Create a wrapper class which returns them through Javabean-style getter methods.
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.