permaculture playing cards*
The moose likes JSP and the fly likes EL - converting types for map key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "EL - converting types for map key" Watch "EL - converting types for map key" New topic
Author

EL - converting types for map key

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

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


Junilu - [How to Ask Questions] [How to Answer Questions]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Where is form.code coming from? Would it not be easier to make sure that it is always the string representation of its value?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4447
    
    5

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

Joined: Jan 10, 2002
Posts: 60810
    
  65

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

Joined: Feb 26, 2001
Posts: 4447
    
    5

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!
 
wood burning stoves
 
subject: EL - converting types for map key
 
Similar Threads
treemap
forEach nested loop
c:set tag with and without body
jstl problem
Struts action form properties