Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

EL [ ] operator...  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a bit hazy about when the contents of the EL [ ] operator are evaluated and when it is treated as an int (say, an index into an array).

HFS says that "If there are no quotes inside the bracket, the Container evaluates whats inside the brackets by searching for an attribute bound under that name...."

However, the following seems to be OK too....

${stuff[0]}

${stuff[1]}

etc.

what are the rules for when the contents are evaluated and when they are simply used as an index ?

Thanks in advance,

Neil.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.${stuff[1]} -> stuff might be an array or List that you can access by an int index

2.${stuff["red"]} -> a map or a bean with a property/key red

3.${stuff['red']} -> a map or a bean with a property/key red

4.${stuff[red]} -> a map or a bean with a property/key with the value that
was found using the pageContext.findAttribute("red"), in
this case if stuff is an array, red migth can be corced
to an int value, otherwise you will get:

javax.servlet.jsp.el.ELException: The "[]" operator was supplied with an index value of type "java.lang.String" to be applied to a List or array, but that value cannot be converted to an integer.
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neil,

In order to be evaluated the thing inside needs to be a valid java identifier (starts with a letter, _ or $ and thereafter can contain numbers).
In your example, 1 is not a valid identifier and so is just treated as a number. Based on the fact you say stuff[0] and stuff[1] work, stuff must be an array of some sort.

If you were to use "1", as in:
stuff["1"]
then the container, being smart, would coerce it to a numeric 1 knowing that stuff is an array and it needs a numeric value to lookup things in arrays. (If stuff were a map, it would simply look up "1" in the map.)

Taking things one stage further - if you were to name it a1, as in:
stuff[a1]
then the container would attempt to find an attribute called "a1" as per Leandro's reply. If "a1" had a value of "0", then this would be substituted and coerced to numeric 0, so the container would look up stuff[0].

Clear as mud?
 
Neil Mc
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you're saying. But if we have, say...

${a["b"]}

...and a is a List. As 'b' is in quotes the container would attempt to coerce it to an int and raise an exception if it couldn't. If 'b' was not in quotes the container would attempt to evaluate it.

...Basically, I'm not sure how in HFS coffee cram for chapter 8, question 6 lists F as a correct answer.

Just concentrating on the 'inner' expression for the timebeing...



where list is an ArrayList and listIdx is an attribute which evaluates to "1".

My point is this... why is 'listIdx' evaluated when it is in quotes ? Why does the container not just see that it is in quotes and try to coerce it to an int (and fail) ?

Any help, much appreciated.

Neil.
[ October 08, 2004: Message edited by: Neil Mc ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!