The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes The book is wrong. one question about 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 "The book is wrong. one question about EL" Watch "The book is wrong. one question about EL" New topic

The book is wrong. one question about EL

Sam Sun
Ranch Hand

Joined: Feb 01, 2005
Posts: 60
First, what's the result of the following EL expression?
"person" is a bean and "notProperty" isn't one of its property.

1. ${person.notProperty}
2. ${person["notProperty"]}
3. ${person[notProperty]}

After done this, you will find this sentence is hard to understand:
(on page 447 of HF, bottom)
But be careful, because the EL expression by itself will NOT cause an exception if the property doesn't exist. So even though: ${fooBean.notAProperty} won't cause an exception by itself(it just return null), if that same "notAProperty" is the value of a "target" attribute, the Container throws an exception.

But after I test, 1 and 2 throws exceptions and 3 outputs nothing.
${person.notProperty} does throw exception. It's different with the book. So the book is wrong.

Please update me.
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
1. ${person.notProperty}
2. ${person["notProperty"]}

1 also encountered error.

Stack trace for 2:

javax.servlet.ServletException: Unable to find a value for "notProperty" in object of class "test.person" using operator "[]".

You are right Bruce

Sawan<br />SCJP,SCWCD,SCBCD<br /> <br />Every exit is an entry somewhere.
Radmika Arunachalam
Ranch Hand

Joined: Mar 29, 2004
Posts: 45
From spec 2.0 page 104,

To evaluate expr-a[expr-b]

� Evaluate expr-a into value-a
� If value-a is null, return null.
� Evaluate expr-b into value-b
� If value-b is null, return null.

e.g. <% pageContext.setAttribute("testname", null); %>
${person[testname]} returns empty.. no exception

If value-a is a Map:
� If !value-a.containsKey(value-b) then return null.

If value-a is a List or array:
� Coerce value-b to int (using coercion rules)
If coercion couldn't be performed: error
� Then, if value-a.get(value-b) or Array.get(value-a, value-b) throws ArrayIndexOutOfBoundsException or IndexOutOfBoundsException: return null
� Otherwise, if value-a.get(value-b) or Array.get(value-a, value-b) throws other exception, error

Otherwise (a JavaBeans object), coerce value-b to String
� If value-b is a readable property of value-a, as per the JavaBeans specification:
� If getter throws an exception: error
� Otherwise: return result of getter call
� Otherwise: error.

In your example,
1) person.notProperty -- notProperty is not evulated it is directly considered as property (value-b) of teh bean and applies above last rule..

2)person["notProperty"] -- same as above

3) person[notProperty] -- here notProperty is considered expr-b and try to find in 4 scopes, since it is not existed, returns null as the value.. here applies first rule given in above spec..

Hope this helps.. let me know if i am wrong..

I agree. Here's the link:
subject: The book is wrong. one question about EL
It's not a secret anymore!