• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL operator doubt?

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

Consider the following code from the HFSJ book,

java.util.Map musicMap = new java.util.HashMap();
musicMap.put("Ambient", "Zero 7");
musicMap.put("Surf", "Tahiti 80");
musicMap.put("DJ", "BT");
musicMap.put("Indie", "Frou Frou");

request.setAttribute("musicMap", musicMap);
request.setAttribute("Genre", "Ambient");

They say that,

Music is ${musicMap[Genre]} evaluates to Music is ${musicMap["Ambient"]}

because there is a request attribute named "Genre" with a value of "Ambient", and "Ambient" is a key into musicMap

I'm not able to follow the bold part as shown above. Anyone please clarify.

I appreciate your help!
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output for ${musicMap[Genre]} will be "Zero 7" because both Genre and musicMap are request scope attributes. EL will try to find Genre in request scope as it tries to find in scopes page then request then session and lastly application.

so Genre will be resolved to its value in request scope i.e. "Ambient". Note that Genre doesn't have single or double quotes in expression ${musicMap[Genre]}. If we make it ${musicMap['Genre']} then Genre will become constant and key to musicMap.

So now the expression will become ${musicMap['Ambient']} and EL will also find musicMap in request scope and will get the value from map against key 'Ambient' which is "Zero 7".

Hope it clarifies
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic