Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

useBean

 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Hope this helps
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the inof but I need some more clarity..

<jsp:useBean id="person" type="Person" class="Employee" >
<jsp:setProperty name="person" property="*" />
</jsp:useBean>

Please explain for this example ..take any attributes as samples
 
mimi chatterjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

The JSP


What is happening ? A bunch of bean reflection classes are used behind the scene to call the setter method. Like this :
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnaks for the clarity info ,,But still I didn't got the answer for my inial doubt in 1st post ..
Please answer it..
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(In my example, "type" is AbstractRancher. "class" is RancherImpl)

1.What is the meaning of "you can set properties in <jsp:setProperty> ONLY on properties on the "type", "

In my example, it means that you can set properties which are defined in AbstractRancher, but not properties which are defined in RancherImpl. But I do not agree with it.

2.what is the meaning of "but NOT on properties that exist only in the actual "class" type."

In my example, it means that you cannot set properties defined in RancherImpl. But I do not agree with it either.
 
mimi chatterjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please clarify me....
Besides of <jsp:useBean tag
How you invoke your setName method of RancherImpl class through a reference of AbstractRancher??Is it possible??
I think its not possible..
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe for your explanation...

chatterjee with the Christophe given example perfectly works..So finally with reflection we can set the attribute of implementaion class with base abstract class.

but again I'm going to basic doubts using reflection we can also access the private memmebers of others class. this is so opposit of OOPs concept ..

I think need some more inputs ....
 
mimi chatterjee
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the point and rectify my idea..
Thanks Christophe..
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Christophe,

I tried your example but with my own class names..

Person.java:

Employee.java:

MyServlet.java:

result.jsp:

I get the following error message:
org.apache.jasper.JasperException: Cannot find any information on property 'empID' in a bean of type 'foo.Person'

so this means person is not able to recognize the property "empID" defined in its subclass Employee.

Correct me if I am wrong

Thanks
Srividhya
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christophe

But when I give the class="foo.Employee" in <jsp:useBean>, <jsp:getProperty> is able to access the getEmpID() defined in the subclass using the superclass reference "person".

Srividhya
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic