• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help me with this

 
sriram kalakoti
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In HFSJ book at page 424 it says

" If the .(dot ) operator is used to access a bean property but the property does not exists, then a runtime exception is thrown."

but on page 395 it says "EL is null friendley and it handles the unknown and null values so that the page still displays, even if it can't find an attribute /property/key with the name in the expression "

iam confused ( iam sure i would have missed something)

if Person is a Bean with just one property "name" then if i say

${Person.age}
will it throw an exception ??? or just doesnt print anything???

thanks
 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By all means if age doesn't exist as the person bean property,

${person.bean} & ${person["bean"]} will both throw exception. I figured this out after trying it out with a program. So, EL is null friendly, but not in all instances.
 
sriram kalakoti
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnaks for your reply But on page 395 it says if there is an attribute bar and it does NOT have an attribute foo then ${bar.foo} will not throw an exception.
how could it be???
 
Sue Pillai
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sriram kalakoti:
Thnaks for your reply But on page 395 it says if there is an attribute bar and it does NOT have an attribute foo then ${bar.foo} will not throw an exception.
how could it be???


I think that page should go to the errata
 
sriram kalakoti
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO i dont think its a typo we are missing something.

can anybody plz help???
 
Liyaquat Ali
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this out:-

I made a Person class with:-

private String name;
private Dog dog;

I made appropriate setter and getters for all properties.

Here Dog is another class that we dont need to worry bout right now.

The servlet creates a Dog object and a Person object and sets the Dog object as the persons dog. Sets the name as something. assigns the Person as a request attribute "person" n forwards the request to a JSP.

now in the JSP :-


${person.name} prints the person objects correct name.
${request.person.name} prints the person objects correct name.

Now this is where it gets interesting:-

${person.age} throws exception, because Person has no 'age' property
${person["age"]} throws exception, because Person has no 'age' property

Now it gets more interesting:-

${request.person.age} it prints nothing.
${request.person["age"]} it prints nothing.

So how is that possible, the behavior is not consistent, its throwing an exception when I dont use the request scope implicit object???

Any suggestions??
 
sriram kalakoti
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow i knew there was something going on.. now can anybody plz help us with this problem???
 
Francisco Moroyoqui
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the latest errata for page 432:

[432] Question 17, Option C;
option C is invalid and should not be checked.
 
Satish Ramasamy
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liyaquat Ali mentioned that
${request.person.age} it prints nothing.
${request.person["age"]} it prints nothing.


Note that there is no implicit object as 'request' in EL. Instead it has 'requestScope' as an implicit object [JSP implicit object 'request' is not the same as this 'requestScope']. In your code, it prints nothing because
1. the EL looks for 'request' in any one of the JSP scopes.
2. It couldn't find any. It returns null.
3. Since EL is null friendly, it promptly prints nothing.

Try using ${requestScope.person.age}, you would throw the same exception.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please check this Post


Thanks
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic