• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mock question regarding EL

 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 3. Given
<% Map m=new HashMap();
m.put(�m�,�n�);
m.put(�n�,�o�);
m.put(�o�,�p�);
pageContext.setAttribute(�map�,m, PageContext.PAGE_SCOPE);


Which of the following will output p ?
1.$ { map.o}
2.$ {map[�o�]}
3.${map.map.n}

Answer is given 1 and 2 . But I want to know why 3 is wrong?





Question 4.
Given
<% Map m=new HashMap();
m.put(�1�,�2�);
m.put(�2�,�3�);
m.put(�3�,�4�);
pageContext.setAttribute(�map�,m, PageContext.PAGE_SCOPE);


Which of the following will output 4 ?
1.${map.3}
2.${map[map[�2�]]}
3.${map[�3�]}
4.${map[map[2]]}

Answer is given 2 and 3 . But I want to know why 4 is wrong?


source ont know exactly from where i noted it
[ June 02, 2008: Message edited by: pradeep singh ]
[ June 02, 2008: Message edited by: pradeep singh ]
 
Ramkumar Rao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 3:

I guess you are bit confused with the [] operator syntax in EL. The [] operator works by evaluating the innermost operations first. But the dot operator expects the right hand operator to be a bean property or Map key. So in this case, the map attribute don't have any key named "map", so its wrong.

Question 4:

Using an integer without quotes inside [] operator works correctly for List and Arrays. In case of Map and Beans, without quotes means, container tries to find the named attribute. In this case container tries to find the attribute named "2" and it fails.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ram,

ok then,why cant be 1.${map.3}(it is satisfies map properties right?)
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use the . operator the thing that follows it should follow Java naming rules. 3 is not a proper Java name for an identifier.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Musab
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankx to you all speciallly to Ramkumar Rao
now i am clear about this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic