获取托管bean,并绑定到"value"类型.属性 [英] Get managed bean and type bound to "value" attribute

查看:62
本文介绍了获取托管bean,并绑定到"value"类型.属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我具有以下结构:

1)托管Bean:

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

    //getters/setters
}

2)测试班:

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);

如何从UIComponent获得绑定到组件的类型?

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

我需要从在组件中设置为值"属性的名称中获取完整的合格类名称.像这样:package.Test.attribute.

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

推荐答案

UIComponent 提供 getValueExpression("attributeName")

样本:

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

NB: Faces 摘自 Omnifaces ,它是"JSF API实用程序方法的集合,主要用于以下方面的快捷方式:从线程本地FacesContext获取内容."

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. "

getType() javadoc

公共抽象类getType(ELContext context)评估相对于所提供上下文的表达式,并返回最可以作为对象传递的对象可接受的常规类型将来对setValue(javax.el.E​​LContext.java.lang.Object)方法的调用中的value参数.这并不总是与getValue().getClass().例如,对于表达式而言,引用数组元素,getType方法将返回数组的元素类型,它可能是该类型的超类当前在指定数组元素中的实际元素.

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.

对于 MethodExpression ,请阅读此内容.

这篇关于获取托管bean,并绑定到"value"类型.属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆