3

I hope that I understood JSF correct and this all makes sense. I try to do some kind of simple templating within a page by using (conditional) includes. The panel is updated by a selection.

<p:outputPanel id="panel">
  <h:panelGroup rendered="#{not empty someBean.selectedObject}">
    <ui:include src="WEB-INF/pages/#{someBean.selectedObject.pageName}.xhtml" />
  </h:panelGroup>
</p:outputPanel>

If I am right the ui:include got processed in some kind of view preparation phase and the rendered attribute just before the page gets rendered. As a result I get a FileNotFoundException because it tries to load WEB-INF/pages/.xhtml. This makes quite some sense to me, but how to solve this problem without a messy hackaround like creating an empty page as a prefix for the filename (page.xhtml) and prefix every page that should be actually rendered with this string (pageSamplePage.xhtml)?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
meistermeier
  • 7,942
  • 2
  • 36
  • 45
  • may Duplicated http://stackoverflow.com/questions/26405708/conditionally-including-a-facelet-file-via-uiinclude/26405938#26405938 – Scorpion Feb 11 '15 at 10:26
  • Don't think that because the page to be included in the linked question exists. The non-existence is my core problem here. – meistermeier Feb 11 '15 at 10:30

2 Answers2

5

You need to conditionally build the <ui:include> instead of conditionally render it. Use <c:if> instead of rendered.

<p:outputPanel id="panel">
  <c:if test="#{not empty someBean.selectedObject}">
    <ui:include src="WEB-INF/pages/#{someBean.selectedObject.pageName}.xhtml" />
  </c:if>
</p:outputPanel>

Otherwise, the <ui:include> still ends up in the component tree.

See also:


Unrelated to the concrete problem, even when you intend to conditionally render parts of the view, you'd better use <ui:fragment> instead of <h:panelGroup> as it has less overhead.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I read the linked post and it makes sense ;) but it does not work :( Now it seems to fall through the if statement. Even a hard-coded false jumps into the wrapped statement. I updated the question with the sample snipplet. Is there any configuration I may check for JSF. I just have one year experience with jsf and I am always fighting with this corner cases. ;) – meistermeier Feb 11 '15 at 10:35
  • What the ...? you are right I can see the `c:if` tags in my html. But I declared `xmlns:c="http://java.sun.com/jstl/core"` in the `ui:composition` tag at the beginning of the 'file' (this page is also just part of the main template in case it matters.) – meistermeier Feb 11 '15 at 11:09
  • Thanks. That was the reason. I filtered the taglib containing jsp as nonsense when my IDE suggested it ;) – meistermeier Feb 11 '15 at 11:38
1

Prevent usage of <c:if>, as it can break some of the standard JSF components. We observed that its usage caused duplication of a child within UI:Panel as a side-effect, and it took a while to identify this as the root-cause.