如果您的唯一目的是以<ui:include>
编程方式使用,那么您应该FaceletContext#includeFacelet()
改为使用。假设您在自定义组件中:
FacesContext facesContext = FacesContext.getCurrentInstance();
FaceletContext faceletContext = (FaceletContext) facesContext.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
faceletContext.includeFacelet(this, "include.xhtml"); // this is your current UIComponent.
这是另一个演示通过命令按钮动态包含的启动示例:
<h:form>
<h:commandButton value="include" action="#{bean.include}" />
</h:form>
<h:panelGroup id="include" />
和
public void include() throws IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
FaceletContext faceletContext = (FaceletContext) facesContext.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
faceletContext.includeFacelet(facesContext.getViewRoot().findComponent("foo"), "include.xhtml");
}