This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The book is wrong. one question about EL

 
Sam Sun
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 250
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Radmika Arunachalam
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..

Radmika
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic