2

I need to access some constants in my jsp, and sadly the EL does not offer any functionality for it. There are some options like the unstandard tag library, but I'd like to keep it a bit more standard.

I tried:

<%@ page import = "com.jackdane.Constants"%>
<c:if test="${object.display == '<%=com.jackdane.Constants.YES %>}'">
//some display logic
</c:if>

But that doesn't appear to do the trick. It's been a while since I've used an expression so I might have made an error. Any input is appreciated.

Edit: To clarify, the constants class is not in my control. It's inside a jar file that I recieved. It contains no getters/setters. Just private static final Strings.

Jack
  • 61
  • 5
  • possible duplicate of [Reference interface constant from EL](http://stackoverflow.com/questions/3732608/reference-interface-constant-from-el) – BalusC Jan 13 '11 at 12:56
  • That topic is about how to access a constant using EL. What I'm asking is where I go wrong when using an expression inside EL. – Jack Jan 13 '11 at 19:10

3 Answers3

2
public class Constants
{

    public static final String YES = "yes";

    public String getYES()
    {
        return YES;
    }


}

jsp

<jsp:useBean id="cons" class="com.abc.Constants" scope="session"/>

<c:if test="${object.display == cons.YES}">
jmj
  • 237,923
  • 42
  • 401
  • 438
  • The java class is not in my controll so that isn't an option. – Jack Jan 13 '11 at 11:25
  • you can't access it from EL without setters/getters – jmj Jan 13 '11 at 12:35
  • I know I need getters and setters. But I was hoping to mix EL and expressions. If I put the expression outside of the quotes and c:if I get the expected value for that constant. – Jack Jan 13 '11 at 12:39
  • Your `test` expression is bogus. The `==` has to go inside the expression. – BalusC Jan 13 '11 at 19:28
0

The answer from Jigar Joshi looks good to me. If you can't modify the constants class, then just wrap it into another one :

public class ConstantsAccessor {
    public String getYES() {
        return Constants.YES;
    }
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • The problem with that is that each time they change it I would also have to update that class. It would be a bit more user friendly if I found a way to get my original snippet to work. (or if they finally added a function to EL for thos) – Jack Jan 13 '11 at 12:31
  • I would personnally prefer to change it in one place (ConstantsAccessor), and detect at compilation time that a constant has disappeared or changed its name, rather than having to change it in all my JSPs, and only discover the problem at runtime. If they change it, you'll have to change things anyway. – JB Nizet Jan 13 '11 at 13:04
0

That topic is about how to access a constant using EL. What I'm asking is where I go wrong when using an expression inside EL.

Scriptlets and EL do not run in the same context. While there are workarounds/hacks, the consensus is that you should not mix them. Use the one or the other. Since scriptlets are discouraged since a decade, EL is the way to go. I'd suggest to use an enum instead. Even though you cannot reference them directly, you can just represent them as strings.

public class Foo {
    public enum Display {
        YES, NO
    }

    private Display display;

    // ...
}

with

<c:if test="${foo.display == 'YES'}">
    ...
</c:if>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555