假设我有以下结构:
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}" />
现在,我知道有一种方法可以查找并获取组件实例:
UIComponent c = view.findComponent(s);
从 UIComponent 中,如何获取绑定到组件的类型?
我需要的是从组件中设置为“值”属性的内容中获取完整的限定类名。类似于:package.Test.attribute。
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 http://omnifaces.org/docs/javadoc/2.5/org/omnifaces/util/Faces.html这是来自全能面孔 http://omnifaces.org/,它是“JSF API 实用方法的集合,主要是从线程本地 FacesContext 获取内容的快捷方式。”
摘录自getType()
javadoc
public Abstract Class getType(ELContext context) 评估
相对于提供的上下文的表达式,并返回最多
接受作为对象传递的对象的一般类型
将来调用 setValue(javax.el.ELContext.java.lang.Object) 方法时的 value 参数。这并不总是与
getValue().getClass()。例如,在表达式的情况下
引用数组元素,getType 方法将返回
数组的元素类型,可能是类型的超类
当前位于指定数组元素中的实际元素。
For 方法表达式 https://stackoverflow.com/questions/8532697/how-to-evaluate-methodexpressions-in-jsf-composite-components读这个。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)