• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on EL

 
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In the below code, I want to know how ${list{'listIndx'+1}} becomes a string. The following is the code:

<% java.util.List.list = new java.util.ArrayList();
list.add("a");
list.add("b");
request.setAttribute("list",list);
request.setAttribute("listIdx,"1");
%>

Can anybody explain me.



With regards,
Padma priya N.G.
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Priya

First you tell me, why should it not be a String.
Any literal quoted between single or double quotes are always considered as String. Isn't it?

Regards,
Khushhal
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
${list{'listIdx' + 1}} is not a valid EL expression. I got a compile error when I try it, something about the second {.

I am assuming you actually meant ${list['listIdx' + 1]}. This results in an EL exception, javax.servlet.jsp.el.ELException: An exception occurred trying to convert String "listIdx" to type "java.lang.Long".

This is because 'listIdx' + 1 is being evaluated first. According to the conversion rules, it tries to convert 'listIndx' string to a number and that causes the exception. Please refer to the spec to look at all the rules and their order.

So to answer your question, ${list['listIdx' + 1]} does not evaluate to a String, it causes an exception.
 
khushhal yadav
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Promod

I am totally agree with you. That would be square brackets [] &.....
But I totally focussed on her main area of concern.

Regards,
Khushhal
[ August 22, 2007: Message edited by: khushhal yadav ]
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Khushal,
Thanks for the explanation.
With regards,
Padma priya N.G.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
How does ${list[list['listIdx']]} evaluates to b?
Can anybody explain?
With regards,
Padma priya N.G.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please clarify the doubt?
With regards,
Padma priya N.G.
 
khushhal yadav
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Priya

${list[list['listIdx']]}



What is list here? An ArrayList. So any string which passed as an index to an ArrayList as an index is automaticlly coerced to an int as per EL rule.
And what is listIdx? It's an attribute of type String having value 1.
So what list['listIdx'](=list["1"]) will evaluate to b as String "1" can be coerced to an int.

But you are once taking that as an index to your list i.e. ${list{b]}.
But now b can't be coerced as an int. So you will get an exception not b as your answer.
Here every thing is all about coercion.

While in your above query, it was about concatenation.

Regards,
Khushhal
[ August 23, 2007: Message edited by: khushhal yadav ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic