Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL Question

 
Reghuram Variyam
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
****My Servlet looks like this*******
response.setContentType("text/html");
PrintWriter out = response.getWriter();

request.setAttribute("0", "AAAA");
HashMap map = new HashMap();

map.put("0", "Zero");
map.put("1", "One");
map.put("AAAA", "VVVV");

request.setAttribute("map", map);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request,response);

**My result.JSP is*************
<html> <body>
<h1 align="center"> From JSP </h1>
<p>

Result from Jsp1: ${map[0]}
<p>
Result from Jsp12: ${map["0"]}

</body>
</html>
*********Result I received is *******
Result from Jsp1:

Result from Jsp12: Zero
*********************************

But I was expecting Result from JSP1 to show "VVVV". Because from ${map[0]}, '0' will evaluate to "AAAA" and then ${map["AAAA"]} will evaluate to "VVVV".
Can some one help me to find out why Iam not getting the result I was expecting ?

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Reghuram Variyam:
'0' will evaluate to "AAAA"


It will not. The numeral 0 will evaluate to just that: the numeral zero.

Doing something silly like trying to create scoped variables or map keys that can can be confused with EL literals will just get you into trouble.
[ January 20, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Please use UBB CODE tags when posting code.

read this
 
Reghuram Variyam
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

According to HF book (page 375), "If there are no quotes inside the brackets, the container evaluates what's inside the brackets by searching for an attribute bound under that name, and substitute the value of the attribute. (If there is an implcit object with the same name, the implicict object will always be used) ".

Please note that, no excpetion is specified for the above rule if what is inside the brackets is a numeric. Is this some thing that need correction in HF book ?
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
****My Servlet looks like this*******
response.setContentType("text/html");
PrintWriter out = response.getWriter();

request.setAttribute("0", "AAAA");
HashMap map = new HashMap();

map.put("0", "Zero");
map.put("1", "One");
map.put("AAAA", "VVVV");

request.setAttribute("map", map);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request,response);

**My result.JSP is*************
<html> <body>
<h1 align="center"> From JSP </h1>
<p>

Result from Jsp1: ${map[0]}
<p>
Result from Jsp12: ${map["0"]}

</body>
</html>
*********Result I received is *******
Result from Jsp1:

Result from Jsp12: Zero
*********************************

But I was expecting Result from JSP1 to show "VVVV". Because from ${map[0]}, '0' will evaluate to "AAAA" and then ${map["AAAA"]} will evaluate to "VVVV".
Can some one help me to find out why Iam not getting the result I was expecting ?

Thanks


You are making a mistake ! if you want VVVV to get printed then here is the code that youe should use -

Result from Jsp1: ${map[map].AAAA} - here the contaiser sees no quotes, evaluates the "map" to "your map" and uses the "AAAA" as a key into that map to retrieve the VVVV, what you expected !

note that, its the 'attributes' that are evalausted by the container and not the map key. you were using a map key with no quotes and expected the container to evaluate it !

hth
guys, correct me if i am worng !
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also note that, what you said -

'0' will evaluate to "AAAA" and then ${map["AAAA"]} will evaluate to "VVVV".

0 evaluates to AAAA, but AAAA is not a map for you to access its contents using EL

so map[0] evaluates to .....map["AAAA"] and you should get VVVV
heyyyyyyyyyyyyy, this is confusnig me tooo...

somebode pleasr clarify !
 
Bob Nedwor
hangman
Ranch Hand
Posts: 215
Eclipse IDE Oracle Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Reghuram.

Even though I am not sure why anyone would want to do it this way, based on the example on page 375, I would have also expected to see

Result from Jsp1: VVVV

Either the example on p. 375 is wrong (see the "This DOES work in a JSP" paragraph), or (like Reghuram said) they need to clarify that this does NOT work with a numeric.

I suggest submitting an errata
It certainly can't hurt.
[ January 21, 2007: Message edited by: Bob Nedwor ]
 
Reghuram Variyam
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to find out how the container would interpret if we provide a numeric inside a bracket when there is an attribute bound in that same name and compare with the rules provided in the book.
I agree that we may not do it this way to get the map value..

So is it true that if we provide a numeric inside the bracket, it will first see whether the attribute to the left side is pointing to a list or array if yes, use the numeric index to retirieve the value and if no, do not evalaute it..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic