Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static fields and EL  RSS feed

 
Bug Menot
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a better way to acces static fields using EL?

i.e.

class Math {
public double sqrt() ...
...
public static final int PI = ...;
}

jsp page:

<c ut value="${Math.PI}" /> <!-- doesn't work -->
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As static fields have no relevance in the JavaBean world, there is no way to access them directly via the EL.
 
Bug Menot
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so we're forced to use scriptlets?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65828
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I never use scriplets. And you cannot use scriplets in some places (within the body of custom actions implementing SimpleTag, tag files, etc). But you can't reference statics directly. You must create an EL-friendly means of access.

For example, let's say there are a number of constants that you want to reference all over the web app. In a context listener you could load up a Map in application context (say, named "Constants") and load the map with entries whose key represents the contant name and whose value represents the contant value.

For example:



henceforth referenced via EL as:



You could even nest Maps within Maps to create a hiearchical constant tree if you wish.
[ May 27, 2005: Message edited by: Bear Bibeault ]
 
Bug Menot
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your the bear. Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!