In HFSJ 414 chapter 8. Bean realted Bullet points.
If you specify a "type" attribute in <jsp:useBean>, you can set properties in <jsp:setProperty> ONLY on properties on the "type", but NOT on properties that exist only in the actual "class" type. (In other words, polymorphosm and normal Java rules apply).
1.What is the meaning of "you can set properties in <jsp:setProperty> ONLY on properties on the "type", " 2.what is the meaning of "but NOT on properties that exist only in the actual "class" type."
Please can any one explain this
SCJP 5 |SCWCD 5| Started thinking about Web Services ?
The type attribute may be an abstract class and the actual class its subclass. So now if you consider polymorphism rules, using base class reference you can invoke methods that are declared in the base class and not only in the subclass.
Hello, according your example, the generated code will be-- Person p = new Employee(); According java polymorphism you cannot access a method by p which is not present in Person but Employee having it. So,in setProperty also, you cannot set a property which is not present in the Person class.
I am waiting for a reply that i am correct or not.. Thanks..
In other words, polymorphosm and normal Java rules apply
I do not agree with this. Bean instrospection (reflection) is used to set the property's value, so even if the bean is declared using the type, the property which is in the subclass will be found and set.
Try this : The abstract class
The concrete class
What is happening ? A bunch of bean reflection classes are used behind the scene to call the setter method. Like this :
The spec doesn't say anything about getProperty. The container seems to be casting to foo.Person and calling getName() on it, which fails. The problem discussed here is about setProperty, not getProperty, but it's interesting to point it out.
For setProperty : JSP.5.2 <jsp:setProperty> The usual bean introspection is done to discover what properties are present, and, for each, its name, whether it is simple or indexed, its type, and the setter and getter methods. Introspection also indicates if a given property type has a PropertyEditor class.
For getProperty: JSP.5.3 <jsp:getProperty> Nothing special. We don't know how the container calls the getter method.
If you use the class attribute, the container is probably casting to Employee before calling the getter method. I can't say much about how the container should behave with jsp:getProperty as I don't find the specification explicit enough.