Given <jsp:useBean id="person" type="foo.Person" class="foo.Employee"> <jsp:setProperty name="person" property="*"/> </jsp:useBean>
In foo.Person we have "name" property & in foo.Employee we have "empID" property.
The request parameter names in the html are "name" and "empID".
The JSP handwriteen say's that "I want you to iterate through the request parameters, and find any that matches this bean's property names and set the value of the matching properties equal to the value of the corresponding request paramaeter." My understanding of the above is that both the properties of the bean (name and empID)will be set.
Is it wright. But in page 414 BULLET POINTS it says that "If you specify a 'type'attribute in <jsp:useBean>,you can set properties in <jsp:setProperty> ONLY on properties of the 'type' ,but NOT on properties that exist only in the actual 'class' type."
since the type of <jsp:useBean> is foo.Person and since it contains only name property. ONLY it will be set.empID will not be set.
Page 360 of HFS is correct. The jsp:setProperty action, when used with property="*", will iterate over all request parameters and attempt to set the property within the JavaBean, irrespective of the 'type' as declared in the jsp:useBean tag. The jsp:setProperty action uses introspection to find the setter methods for each request parameter; and introspection ignores the "declared type" of the object and deals directly with the "actual class" of the object. Make sense?
The penultimate (second to the last) bullet on page 414, is not correct.
However, that bullet is right in one respect that wasn't highlighted in the book. When the jsp:useBean action is used, the JSP page will now include a local variable in the generated service() method, which does follow polymorphic rules.
For example, the JSP code:
Is roughly equivalent to the following scriptlet code:
Notice that the declared type is the superclass, Person, but the created object is of type Employee.
What do you think this code would do?
If you guessed, that a compile-timer error is thrown, then you are correct. Java "typing rules" are in effect even within a JSP page.
Hi All, <jsp:getProperty name="person" property="empId"/> does not work. So we can take the statement "the property accesed depends on type" for jsp:getProperty. But as pointed earlier the jsp:setProperty property="*" sets all the Properties eventhough type does not have the property. <jsp:useBean type="Person" class="Employee">