Search code examples
javajsf-2custom-component

JSF 2.0 Custom Component - how to retrieve object instead of String from Component


I'm creating a custom component whose attribute accepts an instance of an object like this

<hy:bean instance="#{myManagedBean.person}" />

How do I retrieve this instance in my Renderer?

I tried the following but I only get a version of the component converted to a string

Object instance = beanComponent.getAttributes().get( "instance" );

If I do below, i get a NullpointerException

//expecting "#{myManagedBean.person}" which i can then evaluate
String instance = beanComponent.getInstance(); 

this is the definition of getInstance() in BeanComponent

public String getInstance()
{
   return ( String ) getStateHelper().get( PropertyKeys.instance );
}

public void setInstance( String instance )
{
    getStateHelper().put( PropertyKeys.instance, instance );
}

I observed that the Setter is never called.

Any idea how I can get the Object of #{myManagedBean.person} for introspection?


Solution

  • Object instance = beanComponent.getValueExpression("instance").
                        getValue(context.getELContext());
    

    Thanks to this answer Custom component user object value