Reputation: 17002
I am consuming a webservice using my java web application.This returns a number of data among which there is a attribute named Consumer
. This is represented as an object when the WSDL is converted to Java code. There is only one string variable id
inside this class. There is another class Person
which extends Consumer
. This has a number fields like firstName
, lastName
etc. In the JSF code , there is a reference to consumer.firstName
and the value pertaining to firstName
is being printed properly when person object is returned as part of response xml. This confuses me a bit because consumer object does not contain firstName
and still the value is getting printed properly when consumer.firstName
is used. Please can you help me in understanding this.
JSF Code
<h:outputText value="#{myBean.consumer.firstName}" />
Backing Bean
public Consumer getConsumer() {
return consumer;
}
Consumer Class
public class Consumer implements java.io.Serializable {
private java.lang.String id;
public java.lang.String getId() {
return this.id;
}
public void setId(java.lang.String id) {
this.id = id;
}
}
Person Class
public class Person extends Consumer {
private String firstName;
private String lastName;
private String dateOfBirth;
// Getters and Setters
}
Upvotes: 2
Views: 134
Reputation: 691973
JSF uses reflection to call the methods on your objects. It doesn't know what the type of myBean.consumer
is. All it knows is that you asked the firstName
of this object. So, what it does is
So, even if the method getConsumer()
declares that it returns a Consumer
, if, at runtime, the actual object returned by getConsumer()
is a Person
, JSF will search for a getFirstName()
method in Person
, it will find it, and will happily call it and outut its result.
Upvotes: 4
Reputation: 15229
In your JSF page, right before you print the firstName of what you think it's a Consumer instance, also print the value of "consumer.class.simpleName". Most likely this will print "Person" as your object is an instance of "Person" (and it's polymorphically treated as a Consumer type, because in this case a Person instance is also of type Consumer).
Upvotes: 2
Reputation: 13488
When at JSF you refer to consumer.firstName
, it's not the type Consumer
you are accesing, but an attribute of your page/request/session.
This attribute can be set as of a concrete type (Person
) extending Consumer
, and thus it presents a firstName
value.
Upvotes: 2