2

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

Community
  • 1
  • 1
sotix
  • 822
  • 1
  • 8
  • 23

1 Answers1

4

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");
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Well if I could I would vote 50times for that. Worked on first try. Thank you so much! – sotix Jun 01 '12 at 17:54
  • This helped me out... thanks! For those interested: to add some you can use the follow code before including it: `faceletContext.getVariableMapper().setVariable("delegate", FacesUtils.createValueExpression("#{someExpression}", Object.class));` where FacesUtils is a simple helper class. – Daniel Bleisteiner May 24 '17 at 08:32