aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes dot and [] operators in EL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "dot and [] operators in EL" Watch "dot and [] operators in EL" New topic
Author

dot and [] operators in EL

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

With the dot operator, you have to follow the java identifier naming rules. The [] operator is there to overcome this limitation...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.

[My Blog]
All roads lead to JavaRanch
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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
Bartender

Joined: Apr 07, 2010
Posts: 1717
    
  25

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

Joined: Nov 04, 2009
Posts: 2066

Thanks Frits, I've already revised it!
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 515
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


Regards,
Amit
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: May 14, 2004
Posts: 515
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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: Jan 10, 2002
Posts: 61661
    
  67

Because if the property name isn't an identifier how could the parser possibly properly parse the expression? Think about it for a minute.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 515
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

Joined: Apr 18, 2005
Posts: 29
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

Joined: Nov 04, 2009
Posts: 2066

Little bit confused in these! Will come with examples.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1717
    
  25

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

Joined: Nov 04, 2009
Posts: 2066

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dot and [] operators in EL