• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL doubts about no quotes, single and double

 
Ram Gokul
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused about use of quotes in [] . sometimes no quote is used . In HFSJ it says , without quotes it will start searching for an attribute . OK .
If we want to look for Map name , we use double quotes as in
${beername["lite"]} .

Now in question to this chapter ( ie page 428 ) lot of questions have single quote . ${list['listIdx']} vs. ${list['listidx'+1 ]}

Can someone explain this single quote.

Thanks
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In EL you can use double or single quote. Thay have the same meaning.

Thanks
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With EL you can use both single and double quotes. In that question

What happens in that code is when you specify something like

${list[listIdx]}

it looks for an attribute bound under that name and substitutes the value of that attribute which is nothing but String "1". At compilation that String "1" is converted into an integer 1 and starts looking for that corresponding value in the List. Hence 2 is printed.

${list[listIdx+1]}
Here you are adding an integer 1 with 1 so the third element in the list will be printed(index starts from 0) which is "c".

You had mentioned

${list['listIdx']} vs. ${list['listidx'+1 ]}

There is nothing like ${list['listIdx']} in that code because we aren't using a map. If you are using a Map and have a key with this name then this expression is fine.

And regarding ${list['listidx'+1]}

This will produce a compile time error because what ever is inside that square bracket must evaluate to a integer since we are dealing with List and not Map.

Also take a look at the exception message I received

Exception: 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.

Hope now you can understand.
[ November 13, 2005: Message edited by: Vishnu Prakash ]
 
cheenu Dev
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishnu Prakash:
With EL you can use both single and double quotes. In that question

What happens in that code is when you specify something like

${list[listIdx]}

it looks for an attribute bound under that name and substitutes the value of that attribute which is nothing but String "1". At compilation that String "1" is converted into an integer 1 and starts looking for that corresponding value in the List. Hence 2 is printed.

${list[listIdx+1]}
Here you are adding an integer 1 with 1 so the third element in the list will be printed(index starts from 0) which is "c".

You had mentioned

${list['listIdx']} vs. ${list['listidx'+1 ]}

There is nothing like ${list['listIdx']} in that code because we aren't using a map. If you are using a Map and have a key with this name then this expression is fine.

And regarding ${list['listidx'+1]}

This will produce a compile time error because what ever is inside that square bracket must evaluate to a integer since we are dealing with List and not Map.

Also take a look at the exception message I received

Exception: 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.

Hope now you can understand.

[ November 13, 2005: Message edited by: Vishnu Prakash ]



So the option was wrong right???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic