Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing constants using EL

 
Andr� Korsnes Bruns�l
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class called Constants.java that contains a number of static final constants.

When I try accessing them by ${Constants.CONST_NAME}, nothing is returned. I have also tried creating a getCONST_NAME() method in the class but this does not return anything either.

If I use <%= Constants.CONST_NAME %> the correct value is returned.

Is the root of my problem that I have missed something, or can it be some kind of setting on my Tomcat server I have not set?

I'm using Tomcat 5.0.27.

Thanks
 
Stefan Evans
Bartender
Posts: 1687
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope you haven't missed anything.

The getCONST_NAME() method should work though, as long as it is not static. ie public String getCONST_NAME();

Another way to do it is via a map.
Bear recently contributed an article on this: http://www.javaranch.com/journal/200508/Journal200508.jsp#a1

And another post about it:
http://forum.java.sun.com/thread.jspa?threadID=508847
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The EL has no means to access class constants.

There was an extended conversation on this subject a while back in the very forum. If you look back (perhaps a month or os) you may find it.

It is also a subject that is mentioned in this JavaRanch Journal article.

[Bear and Stefan go BUMP!]
[ August 29, 2005: Message edited by: Bear Bibeault ]
 
Andr� Korsnes Bruns�l
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your quick responses Stefan and Bear.

The section on Constants in Bear's article worked like a charm.

Thanks again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic