Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dot and [] operators in EL

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we wants use the dot(.) operator, for example ${first.second}. The dot operator can be used on Maps and JavaBean. If the first is a bean then the second must follow java naming rules for identifiers. Bur, if the first is a Map(the scoped implicit objects), why does the second should follow the java naming rules for identifiers?

Here, in EL, the Maps are implicit objects of the scopes, then the keys are strings, so does it need to follow the Java naming rules for identifiers?

Thanks!
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the dot operator, you have to follow the java identifier naming rules. The [] operator is there to overcome this limitation...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:With the dot operator, you have to follow the java identifier naming rules. The [] operator is there to overcome this limitation...


Thanks Ankit, sorry for asking it again. This is what, I'm asking. If the first is a Map, then why do we need to follow the Java Naming rules for identifiers for the second? To make easy for the translator? Thanks!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the first is a bean then the second must follow java naming rules for identifiers

Where did you read that ? If you have ${first.second}, then :
If first is a Map:
> If !first.containsKey(second) then return null.
> Otherwise, return first.get(second)

If first is a JavaBeans object:
> If second is a readable property of first, as per the JavaBeans specification:
> > If getter throws an exception: error
> > Otherwise: return result of getter call
> Otherwise: error.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré, I'm reading HFSJ, there, for the dot(.) operator,

1) If the expression has a variable followed by a dot, the left-hand variable MUST be a map or a bean.
2) The thing to the right of the dot MUST be a Map key or a bean property.
3) And the thing on the right must follow normal java naming rules for identifiers.


I'm OK with, why we need to follow Java naming rules for the thing in the right of the dot operator for a bean. But I need, if the left hand variable is a Map, then why do we need to follow Java naming rules for the thing in the right of he dot operator? Since the key of the Map is a String, the string can be any literal?

Thanks!
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2531
112
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can actually find it in the specs:

JSP.2.3.1 Overview
...
The . operator can be used as a convenient shorthand for property access when the property name follows the conventions
of Java identifiers
, but the [] operator allows for more generalized access.
...

but the reasoning why is not included.

Regards,
Frits
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Frits, I've already revised it!
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Here, in EL, the Maps are implicit objects of the scopes, then the keys are strings, so does it need to follow the Java naming rules for identifiers?


Let's see one example.
Consider you have set a attribute named "my.name" in session using


Now how would you access this session attribute in JSP using EL.
1) If you use DOT operator then the EL would look like
which is going to fail because the EL Evaluator will think of "my" as a map or bean and the "name" as one of its attribute which is not the case. As you correctly said in a Map<String,Object> you can put any string literal.

2) To overcome this there is a [] operator. Using which you can retrieve the session attribute using


Hope this clarifies your doubt.
Thanks,
Amit

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit punekar wrote:
Let's see one example.
Consider you have set a attribute named "my.name" in session using



I'm not asking about the left hand side part of the dot operator, but, right hand operator! And I also mentioned that, the dot operator should be used with a Map or JavaBean!

Thanks!
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm not asking about the left hand side part of the dot operator, but, right hand operator! And I also mentioned that, the dot operator should be used with a Map or JavaBean!


Would you be able to gives names to the properties(instance fields) of your JavaBean which do not adhere to the Java naming convention standards?

regards,
Amit
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit punekar wrote:Hi,
Would you be able to gives names to the properties(instance fields) of your JavaBean which do not adhere to the Java naming convention standards?


You are correct, we should follow that. I understood it. But, my doubt is, why we need to follow Java naming rules for identifiers for the right hand side part of the dot operator in the case of left hand side is a Map? Could you please give the answer?

EDITED : Correct my typo!
 
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
Because if the property name isn't an identifier how could the parser possibly properly parse the expression? Think about it for a minute.
 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, mu doubt is, why we need to follow Java naming rules for identifiers for the left hand side part of the dot operator in the case of right hand side is a Map?


First of all Right hand side is not Map
. Left hand side is either the MAP or Bean. I guess for Bean you have clarified your confusion.
Considering that left hand side is a map, for e.g sessionScope is a EL implicit object which is of type java.util.Map. You cannot declare a variable name of type map which is not according to Java naming rules. Isn't it?
I think you should post some example which will help the forum to understand your confusion regarding this.

regds,
amit
 
Nitin Kumar
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abimaran,

EL can have arithmetic expression like ${7-foo}

suppose, we have expression like ${first.second-third} , what will we interprate from it? Is it "third" sustracted from first.second or is it "second-third" property of "first" attribute.

To avoid such situation, I think, value after "." has to follow jave naming convention.

So, with naming convention the EL ${first.second-third} means "third" sustracted from first.second.

Regards,
Nitin
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Little bit confused in these! Will come with examples.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2531
112
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I think we still haven't found the real explanation why the right-hand-side of a Map has to follow the rules of a java-identifier if we access it via the dot operator.

The specifications just mention that it is like that. Having read all the replies here, I think it is like that because EL uses the parser for both JavaBeans and Maps. Everything behind the dot operator has to follow the same rules and hence using the the dot operator on a Map is restricted like a JavaBean.

Just an example:

output:
Stringvalue Stringvalue blablabla succeeded Stringvalue

Regards,
Frits
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frits, Thanks for caring it! I think it, with the example like yours. But, if there is a String Literal "my.dog" like your "@notjavaidentifier", then we are in trouble, so they made this strict rules! I totally confused there. But, I think, now it's OK. Wait & See, who will confused me more!

Thanks Frits!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic