This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EL accessing a java Map  RSS feed

 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I almost can use EL, but I'm frequently baffled by things that are probably obvious to others.

I've put a variable into the session scope, a "person" and it has a nice getter that returns a Map. I can't figure out how to access it using the map's keys.

My java code looks like:


and I've tried every combination of EL expressions that I can think of to access it.

The map is keyed on SourceType which is an Enum with values like "home" and "work"



Only the last reference returns something non-null, it returns

map {home=pfarrell@pfarrell.com:home,true}

How do I reference using the key? what I really want is at least


${person.emailMap['home']}

and the goal is to have

${person.emailMap['home'].emailAddress}

resolve to something useful

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your map keys should be strings.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that fixed it.

Is there a way to specify the enums? I always like enums since they cut down on typos and brainos.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably not in JSP EL, which is pretty limited in functionality. Not sure what's in EE6.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65830
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use enums a lot too, but I take their toString() when putting them into such a Map.
 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:I take their toString() when putting them into such a Map.

Yeah, that solves the Java side, not so much the typos and brainos on the JSP author side. Oh well.

I wonder what sort of life EL will have. As David said, its pretty simple, if not simplistic. I think that in five years, this may all look quaint.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!