【发布时间】:2015-07-15 08:37:47
【问题描述】:
我想要一个在 PrimeFaces 3.4 中通过 id 找到 UIComponent 的方法。
我已经找到了一种方法来做到这一点,但它有一个方法 visitTree(在 PrimeFaces 5.2 中可用)不适用于 PrimeFaces 3.4。
请有人帮我在下面的 XHTML 中找到面板对象。
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form id="form">
<p:panel id="pnl"><h:outputText value="Yahoooo...."></h:outputText></p:panel>
<p:commandButton ajax="false" value="Toggle" actionListener="#{myBean.mytoggle}"/>
</h:form>
</h:body>
</html>
Primefaces 5.2 工作方法
public UIComponent findComponent(final String id) {
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot root = context.getViewRoot();
final UIComponent[] found = new UIComponent[1];
root.visitTree(new FullVisitContext(context), new VisitCallback() {
@Override
public VisitResult visit(VisitContext context, UIComponent component) {
if(component.getId().equals(id)){
found[0] = component;
return VisitResult.COMPLETE;
}
return VisitResult.ACCEPT;
}
});
return found[0];
}
【问题讨论】:
标签: jsf jsf-2 primefaces