aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes EL and Exceptions, Clarification required Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "EL and Exceptions, Clarification required" Watch "EL and Exceptions, Clarification required" New topic
Author

EL and Exceptions, Clarification required

Liyaquat Ali
Ranch Hand

Joined: Nov 16, 2005
Posts: 156
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??


Excuse me while I kiss the sky (Jimi Hendrix)
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

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


The above lines print nothing, as the request is not valid implicit Object in the EL. It treated simply as non-existant identifier and the above expression returns null. So fo null value it does not print anything.

It you really test the behavior of non-existant properties Test:

${requestScope.person.age}
${requestScope.person["age"]}

This wiil search the person bean in Request Scope and throw Exception for non-exitstant property of the person bean.

Hope this help you.

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Liyaquat Ali
Ranch Hand

Joined: Nov 16, 2005
Posts: 156
Thanx Narendra,

Thanx for the answer, it has been a gr8 help


I tried another thing...

${requestScope.X} prints nothing... (as there is no property named X)
${requestScope.Scope.X.Y} prints nothing... (as there is no property named X)
${X} prints nothing... (as there is no property named X)
${X.Y} prints nothing... (as there is no property named X)


but is there is a attribute named SomeThing which does not have a Property P this is wat happens:-

${Something.P} will throw a Exception

So if EL finds an attribute but does not find the required property then an exception is thrown, if the attribute is not found altogether then no exception is thrown.
[ March 02, 2006: Message edited by: Liyaquat Ali ]
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

If the attribute is not found,i.e. it is null. So it print nothing.
When you use Something.P, If either Something or P is null the expression is evaluated to null.

Thanks
Liyaquat Ali
Ranch Hand

Joined: Nov 16, 2005
Posts: 156
Thanx again Narendra!
Xiaoxiao Lam
Greenhorn

Joined: Mar 04, 2006
Posts: 27

When you use Something.P, If either Something or P is null the expression is evaluated to null.

Narendra, do you mean if "Something" is a valid attribute and it doesn't have a property named "P", ${Somthing.P} will print out nothing and no exception is thrown? Just want to make sure. I got confused after reading the posts.

Thank you.
[ March 05, 2006: Message edited by: Xiaoxiao Lam ]

SCJP 1.4<br />SCWCD 1.4
Liyaquat Ali
Ranch Hand

Joined: Nov 16, 2005
Posts: 156
No its not like that....

read my post above where I mentioned what I tried out.

This is the way it wox:-

Assume EL is expression is ${Something.P}

In this case if Something is an attibute in any scope AND if it does NOT have a property named P then a Exception is thrown.

Also, if there is no property named Something in any scope then no Exception is thrown and nothing is printed.

I know its confusing but try it out UrSelf, write a couple o beans n a few EL lines in a JSP n C wat happens
Xiaoxiao Lam
Greenhorn

Joined: Mar 04, 2006
Posts: 27
I see. Thank you!
Xiaoxiao Lam
Greenhorn

Joined: Mar 04, 2006
Posts: 27
I am confused now again when I read P447 on HFSJ where it says ${fooBean.notAProperty} won't cause an exception. Anyone can explain? Thank you.
 
jQuery in Action, 2nd edition
 
subject: EL and Exceptions, Clarification required