0

Using JSP and JSTL, I am trying to import a Constant class into my template. At the top of my file, I have:

<%@ page import="app.util.Constant" %>

This throws no error, but I'm pretty sure the file is compiling correctly, because I get a compilation error if I try:

<%@ page import="app.util.DoesntExist" %>

Despite this working, I can't reference the Constant class or properties on it:

<c:if test="${Constant == null}">
    <c:out value="foo"/>
</c:if>
<c:if test="${Constant.LOGIN_URL == null}">
    <c:out value="bar"/>
</c:if>

Both foo and bar will be printed. How do I reference this class without resorting to <%= Constant.LOGIN_URL %>?

In my Constant class, I have:

public static final String LOGIN_URL = "login";
public static final String getLOGIN_URL() {
    return LOGIN_URL;
}

Thanks in advance.

jds
  • 7,910
  • 11
  • 63
  • 101
  • `${Constant.LOGIN_URL == null}` means: find a page, request, session or application attribute named "Constant" call the instance method getLOGIN_URL() on this object (if any), and compare the result with null. It doesn't do what you want AT ALL. You can't refer to constants inside the JSP EL (unfortunately). – JB Nizet Aug 01 '15 at 16:40
  • I'm confused, because that's exactly what I want. I'd like JSTL to call `getLOGIN_URL` on the Constant class. This isn't possible? I say it isn't working because I have some working code for another project that does just this—unless I'm completely misremembering. – jds Aug 01 '15 at 16:52
  • No, it's not possible. What is possible is to have an **object**, stored in an **attribute** under a given name, and call an **instance** getter that would return the constant. – JB Nizet Aug 01 '15 at 17:09

0 Answers0