How do you reference an interface constant 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 interface constant 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 can't. It follows the Java Bean convention. So you must have a getter for it.
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}
This is not possible using standard EL. There are however several ways to achieve the requirement:
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:
<%@ 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}
.
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}
as well.