• 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

EL - converting types for map key

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following EL, lookup is a Map that has Strings as keys. The expression will use form.code to look up the the corresponding description.

${lookup[form.code].longDescription}

This works fine if form.code is a String. However, in some instances, form.code will be a primitive long or int, in which case the lookup will fail because there is no type-coercion with map keys. Is there a way, aside from changing the Map key type, to get the lookup to work with a primitive int or long value? I've experimented with c:set but this only changed form.code from primitive long to java.lang.Long.

<c:set var="code" value="${'' + form.code}" />

didn't work for me either: the code variable will still be a java.lang.Long.

TIA
 
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
Where is form.code coming from? Would it not be easier to make sure that it is always the string representation of its value?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the other limitation: form.code can be a non-String value. It would be a lot simpler if we could force form.code to be always a String but we can't.

The workaround/kludge I've come up with at this point is this:



There must be an easier/cleaner way to do this...
[ October 05, 2004: Message edited by: Junilu Lacar ]
 
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
If you are using JSP2 and JSTL1.1 you could look into defining an EL function to do this -- but I know where you are coming from with wanting a cleaner solution.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to program to JSTL 1.0. So far, I've refactored to reduce to two tags in the JSP:



The code for RefDataWrapper is a simplification (actual code ensures that the Map keys are Strings).

Any suggestions for further refactoring are welcome. Thanks!
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic