How to use the IncludeHandler inside my custom component?

别来无恙 提交于 2019-12-06 10:50:44

问题


In question: The class behind ui:include JSF tag I found out that I need to use the IncludeHandler to use

<ui:include>

programmatically. However, the constructor needs a "config"-parameter and I don't know how to set this up.
Please give an example that shows how to use the IncludeHandler for a simple include like

<ui:include src="include.xhtml" />

My jsf-component currently is built programmaticly but I want to include some parts written as ".xhtml". So at the end a web-designer simply has a component like

<fg:generator></fg:generator>

and some ".xhtml"-files to play around with the styling. If there's a better approach than the IncludeHandler (still needs to be in Java) let me know :)


回答1:


If your sole purpose is to use <ui:include> programmatically, then you should be using FaceletContext#includeFacelet() instead. Assuming that you're inside your custom component:

FacesContext facesContext = FacesContext.getCurrentInstance();
FaceletContext faceletContext = (FaceletContext) facesContext.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
faceletContext.includeFacelet(this, "include.xhtml"); // this is your current UIComponent.

Here's another kickoff example which demonstrates dynamic include by command button:

<h:form>
    <h:commandButton value="include" action="#{bean.include}" />
</h:form>
<h:panelGroup id="include" />

with

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


来源:https://stackoverflow.com/questions/10853177/how-to-use-the-includehandler-inside-my-custom-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!