0
votes

Let's suppose I have following structure:

1) Managed Bean:

@ViewScoped
@ManagedBean
public class TestBean {
    private Test test;

    //getters/setters
}

2) Test class:

public class Test {
    private String attribute;

    //gets/sets
}

3) XHTML

<p:inputText id="test" value="#{testBean.test.atribute}" />

Now, I know there is a way to find and get component instance:

UIComponent c = view.findComponent(s);

From UIComponent, how do I get the type bound to component?

What I need is to get full qualified class name from what is set as "value" attribute in component. Something like: package.Test.attribute.

1
Since ui know it's an InputText from primefaces can't you simply do InputText c = ((InputText) view.findComponent(s)).getValue().getClass()?Mário Fernandes
Hello @MárioFernandes, thank you for your answer. I did what you suggested but it is throwing java.lang.ClassCastException: javax.faces.component.UINamingContainer cannot be cast to org.primefaces.component.inputtext.InputTextEmilio Numazaki
I'm assuming you're finding the incorrect component then. Since p:inputText is not a UINamingContainer. Are you passing the full id (maybe there needs to be the form id preprended)Mário Fernandes
Yes, I'm using PrimeFace's ComponentUtils.findComponentClientId utility method. My guess it is returning composite instead of inputText but it doesn't make sense since composite have no ID.Emilio Numazaki
I see that the findComponentClientId uses a method firstWithId, do you have anything with the same id in the dom? (not the same since it's not allowed but starting the same at least)Mário Fernandes

1 Answers

1
votes

UIComponent offers getValueExpression("attributeName")

sample :

  UIViewRoot viewRoot = Faces.getViewRoot();
            UIComponent component= viewRoot.findComponent("x");
            ValueExpression value = component.getValueExpression("value");
            Class<?> expectedType = value.getType(Faces.getELContext());

NB:Faces here is from Omnifaces, which is a "Collection of utility methods for the JSF API that are mainly shortcuts for obtaining stuff from the thread local FacesContext. "

excepts from getType() javadoc

public abstract Class getType(ELContext context) Evaluates the expression relative to the provided context, and returns the most general type that is acceptable for an object to be passed as the value parameter in a future call to the setValue(javax.el.ELContext. java.lang.Object) method. This is not always the same as getValue().getClass(). For example, in the case of an expression that references an array element, the getType method will return the element type of the array, which might be a superclass of the type of the actual element that is currently in the specified array element.

For MethodExpression read this.