Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HFSJ page- 395

 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,

I have one doubt that you people can clarify.

It is written that EL is null - friendly, page displays even if it can't find an attribute/property/key with the name in the expression.

Following can be scenarios.....>
1. Suppose if foo attribute is not present in any of the scope
${foo} will displays nothing and no exception will come
${9+foo} will be 9 as in Arithmetic exp. null is 0
${true && foo} will be false as in Logical exp. null or any other value other than true will be false.

2. Suppose we have bar but bar has no property.
here bar attribute is a bean and do not have any property like age

${bar} will displays called toString of bar bean.
BUT
${bar.age} will throw exception where it is mentioned in book that it
${bar[foo]} will display nothing but exception...

Please clarify this doubt that if property does not exist, should there be nothing on page or exception...

What i have tested, i think, property must be there and if property is null, then nothing is displayed...

Have a nice day.........

Pawan
SCJP 5.0
Target: SCWCD 1.4
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look if theres an attribute called bar and if bar have some property called name then ${bar.name} returns the value of name
eg:- Name:${bar.name} gives output as Name:ruquia
if name has value ruquia.

Now when bar itself do not exist then
${bar},${bar.name} returns null....that is nothing is printed and a blank browser appears.
This is for the benefit of user that instead of popping up some exceptions and scaring the user the EL gives us a blank or returns null(note that i am saying returns null it wont print null)
Hope you got it.
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what will be the output if foo is there but it does not have any property like "noProperty"


then by saying ${foo.noProperty} will throw an exception that noProperty attribute not found in foo.

If foo exits in any scope and it does not have property like "noProperty", tomcat is throwing exception that is opposite what is mentioned on page-447 (last point in key points on c:set)

Please correct me if i am wrong.
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what will be the output if foo is there but it does not have any property like "noProperty"

If foo exists and "noProperty" dont exists then it returns null ....
In page 447 it is saying if target="${attributeobject}" and if this attributeobject do not exists the container throws exception.
also if target resolves to an attributeobject or real object of a Map or a Bean and it defines a property which do not match with the existing properties(or as told notaproperty) of map or bean then it throws exception.
${foo.notaProperty}-------null
<c:set target="${fooBean}" property="notaProperty" value="xx"/>--exception

Anybody correct me if i am wrong..
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are perfectly correct, But what if we try it without <c:set target option.

Just display it using C ut... book says that nothing displayed, if bean attribute exists but with no property.. so

${beanAttr.noProperty} will also throw exception...but book does not say it.
 
Sandeep Krish
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${beanAttr.noProperty} will throw exception but ${beanAttr[noProperty]} will not. Right? This was a question for me as well. Anyspecific reason? Spec is also lil confusing.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic