Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL and missing properties

 
Oliver Rensen
Ranch Hand
Posts: 109
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,

there are many interesting discussions in this forum about when EL throws an ELException, and the behaviour of EL seems clear to me, but now I have read the chapter about <c:set> in the Head First book, and I am a little confused. Head First wrote on page 447:

If the "target" expression is a bean, but the bean does not have a property that matches "property", the Container throws an exception. 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 returns null), if that same "notAProperty" is the value of a "target" attribut, the Container throws an exception.

IMHO there are two errors in this section:

a) The EL expression by itself will NOT cause an exception if the property doesn't exist.

This is wrong, because the EL will throw an exception if the bean exists, but the property doesn't exist.

b) ${fooBean.notAProperty} won't cause an exception by itself (it just returns null).

This is wrong, because if fooBean exists and notAProperty doesn't exist, the EL will throw an exception, isn't it?

Only if fooBean doesn't exist, it just returns null.

Regards
Oliver
 
Alex Pol
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you write ${foo}, and foo is not found in any scope, nothing will be printed.
If you write ${foo.bar} and foo is found, but bar is not the name of a property, then you will have an exception, because the translated servlet tries to call getBar(), and you will have a compile-time error, i think.

But if foo is a Map, I don't think ${foo.bar} throws an exception. It just doesn't find that key, and prints nothing.
 
Oliver Rensen
Ranch Hand
Posts: 109
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds good to me.
Thank you for the help.
 
khushhal yadav
Ranch Hand
Posts: 242
Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oliver,

You can remember it in this way also, exceptions are never thrown in case of Map. At most you will get empty output on your screen.

Exceptions are always raised in case of beans, if the accessed property is not defined in the bean. Otherwise it will return the result of getter call.

Regards,
Khushhal
[ August 30, 2007: Message edited by: khushhal yadav ]
 
Oliver Rensen
Ranch Hand
Posts: 109
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khushhal, thanks a lot for the clarification.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic