• 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

Sharing constants inside JSP

 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings,

I defined a public static final constant in a Struts Action. If I want to use that inside a JSP; how would I declare that ? Note that I do not want to use scriptlets in my JSP; but JSTL or any of the Struts's tags is fair game.

Thanks

Pho
[ July 12, 2005: Message edited by: 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
What is it you are trying to do with said constant in the JSP page? You will not be able to reference it with the EL.
 
Pho Tek
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a hidden text input set to a constant string.
In my struts action; I will read that hidden form value and perform
some decision tree switching.

The final code in the JSP should be something like this:
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
What is it you are trying to do with said constant in the JSP page? You will not be able to reference it with the EL.



Is there a way of refering to Constants using the EL?. The only idea I have is to have a Constants class as a JavaBean, but this will lose the idea of a Constants class.

I really like JSTL and have been using it a lot. The EL is very powerful and since i have to use J2EE 1.3 I'm stick with JSTL 1.0 so can't use advance JSTL elements like functions. Still find it very helful.
But I find that without accesing constants then it is very error prone.

I mean, having a like like this:
<c:set var="miVar" value="${requestScope.myBean}" />

in a lot of pages would be a pain if suddenly the bean is stored in the requestScope under another name rather than "myBean".

Thanks in advance
 
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, there is no way to directly access class-level constant values from the EL.

One technique that you could use is to place a Map instance at the application level that contains values that can be shared across pages.

For example, in a context listener you could have something along the lines of:



Which means that on-page, you could reference them via something like:

 
Jorge Blaz
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear.
Yes, this could be a nice solutiom. I've constants grouped in several classes, so i would have to use several maps or a map o maps, and have as keys some id that would identify each class.
 
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
Yeah, I'm not sure if I'm a big fan of the technique on a large scale. But breaking up the namespace with multiple Map instances is a good instinct on your part.
 
Jorge Blaz
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if it's not taking with care having a lot of maps for storing the constants can be a big mess. But if the namespaces are clear, maybe it's easy to have them accessed in an easier way and at the same time have the advantage of maintanance that constants give you. I will take a look at it and see if i can apply on the project.
Thanks for your info Bear, it has been very useful.
 
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
Glad to help, but also be aware that this technique doesn't completely shield you from typos. If you misspell the map name, the EL will loudly complain. But if you misspell the key, it will just return a blank. Granted, that sticks out more (making it easier to find) than a mere typo would, but it's not as satisfying as a good old compile-time error.

The inability to directly accesss constant values is about the only thing I miss after going completely sciptless. But the other benefits I reap vastly outweigh that disadvantage.
 
Jorge Blaz
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
The inability to directly accesss constant values is about the only thing I miss after going completely sciptless. But the other benefits I reap vastly outweigh that disadvantage.



Agree 100%
reply
    Bookmark Topic Watch Topic
  • New Topic