I think, therefore I blog

Access Java Static Methods and Constants from EL

By , 24 September 2015

Access Java Static Methods and Constants from EL
Access Java Static Methods and Constants from EL

Java EL 3.0 provides a way to access static variables and static methods from JSF or JSP, but it's a bit nasty, requiring you to import the packages into your EL Context. I'm lazy and have been using this hack since 2012, so I'm just going to keep doing it for EL 3.0. The hack I'm using makes static methods and constants available via normal managed beans. Note, I'm using Lombok annotations to generate the getters and setters automatically.

@ManagedBean
@ApplicationScoped
@Getter @Setter 
public class App {
    
    /* global compile-time constants */
    public static final String ROOT_DIR = "/var/local";
    public static final String UPLOADS_DIR = "/var/local/uploads";
    public static final String IMAGE_DIR = "/var/local/images";
    public static final String MANAGER_ROLE = "manager";

    /* EL accessors for static constants */
    String root_dir = ROOT_DIR;
    String uploads_dir = UPLOADS_DIR;
    String image_dir = IMAGE_DIR;
    String manager_role = MANAGER_ROLE;

    /* EL accessors for static methods */
    public long ctime() {
       return System.currentTimeMillis();
    }
}
Access Java Static Methods and Constants from EL

Java identifiers are case sensitive so there is no namespace problem with the static and non-static properties. Now, all we need to do to access the statics via EL is:

 ${app.manager_role}

or

 ${app.ctime()}

As an added bonus, your IDE should be able to refactor your templates easily if you decide to change the variable names.

Alternatively, you could declare all your static methods in faces-config.xml but that's too much work for me, and you lose type safety and refactoring support.

 

About Roger Keays

Access Java Static Methods and Constants from EL

Roger Keays is an artist, an engineer, and a student of life. Since he left Australia in 2009, he has been living as a digital nomad in over 40 different countries around the world. Roger is addicted to surfing. His other interests are music, psychology, languages, and finding good food. Click here to subscribe to his weekly blog, or stalk him on Facebook and Twitter.

Leave a Comment

Please visit https://RogerKeays.com/access-java-static-methods-and-constants-from-el to add your comments.

Comment posted by: bat, last year

Muy bueno

Comment posted by: kenneth centurion, last year

you can also display values from the facescontext sesionmap like this:

<p:outputLabel value="Last Logged In:[#{sessionScope.lastLoginDate}]"/>

 

and in your bean you can put a value in the session map like this:

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("lastLoginDate",new Date());

Comment posted by: , 4 years ago

That works too, but I actually like how the application scoped bean creates a mini namespace for my constants.

Some people also use the "map hack". i.e. override the get() method of a Map.

Comment posted by: Tom Asel, 4 years ago

 Nice one :-)

Another approach is to have a custom ELResolver enabling you to use custom implicit objects.

E.g. SomeClass.MY_CONSTANT could be accessed via #{myConstant} . I prefer this one over application scoped beans for my projects.

Join 3,559 People Who Think Outside The Box

I write every Sunday about travel, psychology, technology, and all sorts of interesting stuff. It's completely free, and you can subscribe for as long as you like. Do it now, so you don't miss a single post.

Chat For A While

Your Vote Matters

Which animal will take over when humans go extinct?
Lions
Dogs
Pigs
Octopuses
Cockroaches
Rats
Dolphins
Sharks
Jellyfish
Cows

Read a Good Book

“A spellbinding true story of love, passion and adventure. One can’t help but be swept away by 100% Love Guaranteed.” —Dr Tammie Matson, author of Elephant Dance.