views:

60

answers:

3

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?

+2  A: 

You can't. It follows the Java Bean convention. So you must have a getter for it.

Adeel Ansari
+2  A: 

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}

Bozho
Bit of a novice here when it comes to jsp's- could you explain that more fully?
tau-neutrino
@tau-neutrino: Its simple actually. Create a class with `url` as a String property, name it `Configuration`, instantiate it and set the `url` to whatever you like. After that set that `Configuration` object in `ServletContext`. Do something like, `servletContext.setAttribute("config", config)`. And there you go.
Adeel Ansari
+1  A: 

This is not possible using standard EL. There are however several ways to achieve the requirement:

  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:

    <%@ 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}.

  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} as well.

BalusC