• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

The book is wrong. one question about EL

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic