sotix
sotix

Reputation: 822

How to use the IncludeHandler inside my custom component?

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

Upvotes: 2

Views: 1432

Answers (1)

BalusC
BalusC

Reputation: 1109532

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

Upvotes: 4

Related Questions