• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

static fields and EL

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 -->
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so we're forced to use scriptlets?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your the bear. Thanks!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic