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

page 360 if property="*"

 
Satish Saravanan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In page 360,

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.

Is it Wright.

PLEASE explain me.
satish
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also confused.
This code also working.


<%@ page import="foo.*" %>
<html>
<body>
<jsp:useBean id="person" type="foo.Person" class="foo.Employee">
<jsp:setProperty name="person" property="name"/>
<jsp:setProperty name="person" property="empID" value="<%= new Integer(request.getParameter("empID")).intValue()%>"/>
</jsp:useBean>
<jsp:getProperty name="person" property="name"/>
<jsp:getProperty name="person" property="empID"/>
</body>
</html>
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Satish,

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.

HTH,
Bryan
[ December 05, 2006: Message edited by: Bryan Basham ]
 
Anupama Ponnapalli
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bryan,

Is this (page 414 bullet) going to be included in the errata?

I could not find it here: http://www.oreilly.com/catalog/headservletsjsp/errata/headservletsjsp.confirmed

There have been a number of posts on this topic, and it will definitely help.

Thanks,
Anu
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Anupama,

I have submitted an errata entry for this mistake.

Thanks for the suggestion,
Bryan
 
Vinod Subramaniyam
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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">
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic