*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes right to the dot operator should be a bean property? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "right to the dot operator should be a bean property?" Watch "right to the dot operator should be a bean property?" New topic
Author

right to the dot operator should be a bean property?

Brian Percival
Ranch Hand

Joined: Jun 23, 2004
Posts: 163
chapter 8, p423, q 15.

public class MyBean {
private java.util.Map params;
private java.util.List objects;
private String name;
public java.util.Map getParams() {return params;}
public String getName() {return name;}
public java.util.List getObjects() {return objects;}
}
Which will cause errors( assume that an attribute named mybean can be found, and is of type MyBean)? (Choose all that apply.)

a)${mybean.name}
b)${mybean["name"]}
c)${mybean.objects.a}
d)${mybean["params"].a}
e)${mybean.params["a"]}
f)${mybean.["objects"].a}

MyBean doesn't have a property "objects" or "name" or "params" coz no valid setter/getter methods for those properties.

So shouldn't all of them throw errors??? What is it that I am missing? Can someone plz explain?


regards,
Brian

[ July 10, 2005: Message edited by: Brian Percival ]
[ July 10, 2005: Message edited by: Brian Percival ]
shiva viswanathan
Ranch Hand

Joined: Aug 12, 2004
Posts: 152
HI Brian ,

You are right in the sense that if we call a setter method then it would have definitily thrown an error .

But here the expressions result in calling only the gettter methods and hence
it works.

Whenever we output any expresion value then only the getter methods are called

If we had tried <jsp:setProperty> then it wouldnt have worked

Catch You Later
Shiva
Brian Percival
Ranch Hand

Joined: Jun 23, 2004
Posts: 163
I didn't get it.. P 368 states under "using the . operator" that

"the thing to the right of the dot is either a map value or a bean PROPERTY if the first variable is an attribute that's a javabean.

But if something is a bean PROPERTY, doesn't that mean by definition of a property that it should have corresponding getx,setx methods? So then how come the "objects" etc in the MyBean class are considered properties of the bean???

regards,
Brian
shiva viswanathan
Ranch Hand

Joined: Aug 12, 2004
Posts: 152
Hi Brian ,

You are confusing Bean Law and JSP Law

By bean law it is required for the bean to have getter and setter methods
And JSP law advocates that beans strictly follow this rule
so that we dont get an error when we do setProperty for eg

But there is no check in JSP itself to actually validate that the
bean being displayed follows the rules .You will get the error at run time

Hence in this case it has getter methods and we are accessing only the
getter methods when we do something like
beanname.property which will internally call
beanname 's getProperty method

In fact you can try this on any variable (even if it is not a java bean )but if it doesnt have the
getter method then nothing will be displayed

There is no check in JSP specs itself to ensure at complie time that
name on the left is a bean or map

Hope you get it

Thanks
Shiva
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: right to the dot operator should be a bean property?