• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL - converting types for map key

 
Junilu Lacar
Bartender
Posts: 7600
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 7600
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 7600
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic