I'm fully aware of the introspection/reflection api's in the sdk, but
this is not what I want, at least I hoped some support for
OO/Inheritance would be availlable in JSF
in plain java the problem wouldn't even need reflection/introspection!
I would be able to just use the instanceof operator in an if-statement
and cast the object to the needed type
in jstl this is also not a problem either:
${!empty bean.method}
will not fail is the bean doesn't have the requested property, this
counts as 'empty'
So, in your opinion, the only way to solve this problem would be:
write a custom component for rendering text which encapsulates the
exeption thrown by invocating a non-existent method (or tests if the
method is callable or not, which in most cases would be less
efficient) ....?
regards,
Peter
On 11/24/05, Craig McClanahan <[EMAIL PROTECTED]> wrote:
> On 11/24/05, Peter Maas <[EMAIL PROTECTED]> wrote:
> > Yep,
> >
> > allready tried that.... errors on the fact the the specific object doesn't
> have the requested property.... maybe I'll need to add an additional domain
> object have ALL the methods and convert the underlying objects on the
> backingbean...
>
> Peter,
>
> The APIs you are looking at to introspect properties in a class are the
> same ones that a JSF implementation uses as it is processing value binding
> expressions ... the bean introspection APIs that are built in to the JDK.
> The APIs are all documented in the JavaBeans specification (for more
> information start at
> <http://java.sun.com/products/javabeans/>), but it goes
> something like this:
>
> BeanInfo beanInfo = Introspector.getBeanInfo(Customer.class);
> PropertyDescriptor props[] = beanInfo.getPropertyDescriptors();
>
> This gives you an array of descriptors for all the JavaBean properties
> defined in this class, including Method objects pointing at the getter and
> setter methods so you can use reflection APIs to call them dynamically.
>
> If this is all a bit low level for your taste, you can also explore the
> APIs in Commons BeanUtils
> (http://jakarta.apache.org/commons/beanutils), which is a
> dependency of both the MyFaces and RI JSF implementations (so you'll have it
> available in your apps already).
>
> Craig
>
>
>
> >
> >
> > On 11/24/05, Marius Kreis <[EMAIL PROTECTED]> wrote:
> > > Good point. You could use
> > > <h:outputText value="#{user.address}" rendered="#{ user.class.name eq
> > > "Customer"}">
> > > But I doubt that it works... I think the expression wil be evaluated
> > > first and then throw an exception... sorry.
> > >
> > > Peter Maas wrote:
> > > > I understand that this is possible, but how can I test this within a
> > > > tableGrid component without jstl?
> > > >
> > > > On 11/24/05, *Marius Kreis* < [EMAIL PROTECTED]
> > > > <mailto:[EMAIL PROTECTED]>> wrote:
> > > >
> > > > Hi Peter.
> > > > Perhaps you can check for the class of the object with
> > > > user.class.name <http://user.class.name>
> > > > I think this should query user.getClass().getName() .... and then
> you
> > > > can compare it to "User" or "Customer"....
> > > >
> > > > Peter Maas wrote:
> > > > > Hi all,
> > > > >
> > > > > I'm probably missing something obvious, but how can I 'detect'
> the
> > > > > existence of a property on a bean? I have a managed bean return
> a
> > > > list
> > > > > of users, which might can be a plain user or an extended
> 'customer'
> > > > >
> > > > > class User{
> > > > > String firstname;
> > > > > String lastname;
> > > > > String email;
> > > > >
> > > > > // getters and setters
> > > > > }
> > > > >
> > > > > class Customer extends User{
> > > > > String address;
> > > > > // etc.
> > > > > }
> > > > >
> > > > >
> > > > > Now if I want to mix those objects in one single dataTable I
> > > > cannot find
> > > > > a way to display properties from
> > > > > customer objects.... the code will break on plain user objects,
> and
> > > > > complains about missing properties.
> > > > >
> > > > > I tried using jstl inside the table, but I'm not sure how the
> 'test'
> > > > > properties of iterated items in the dataTable... a simple
> ${!empty
> > > > > user.address } doesn't work.
> > > > >
> > > > > Actually, I would prefer to NOT use jstl..... but stick to
> > > > JSF.... is it
> > > > > possible with the existing tags?
> > > > >
> > > > > regards,
> > > > >
> > > > > Peter
> > > >
> > > >
> > >
> > >
> >
> >
>
>