Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism Rules and the <jsp:useBean> tag

 
Meenakshi Sahu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



The polymorphism rules must work in the jsp as well while we are using the <jsp:javaBean> tags.I read in one book as well that : " IF YOU SPECIFY THE 'TYPE' ATTRIBUTE IN JSP <JSP:USEBEAN> YOU CAN SET THE PROPERTIES IN <JSP:SETPROPERTY> ONLY ON THE PROPERTIES OF THE 'TYPE' , BUT NOT ON THE PROPERTIES THAT EXIST ONLY IN THE ACTUAL 'CLASS' TYPE.(IN OTHER WORDS THE POLYMORPHISM AND JAVA RULES APPLY.)


I tried to test this using the below jsp :

<BODY>
<jsp:useBean id="person" type="principal.Person"
class="principal.Employee" scope="request">
<jsp:setProperty name="person" property="name" value="Meenakshi" />
<jsp:setProperty name="person" property="empId" value="10063865" />
</jsp:useBean>
Employee Name:<jsp:getProperty name="person" property="name" />
Employee ID:<jsp:getProperty name="person" property="empId" />
</BODY>


And my Person and Employee classes are as below :

public abstract class Person {
String name = "";
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}



public class Employee extends Person {
private Integer empId;
public Integer getEmpId() {
return empId;
}

public void setEmpId(Integer empId) {
this.empId = empId;
}

}



However it allows me to set the values for both the name and the empId attribute, though the empId is not present in teh Person class.
And i get the output on my page as Employee Name:Meenakshi Employee ID:100.
Can anyone explain me why its happening so?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Meenakshi,

Welcome to java ranch. Please do not use ALL CAPS when writing a post. It will appear like you are shouting. Please put your code inside the CODE tags so that they are readable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic