Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional include with empty value in JSF

Tags:

jsf

primefaces

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

like image 728
meistermeier Avatar asked Dec 01 '25 02:12

meistermeier


1 Answers

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:

  • JSTL in JSF2 Facelets... makes sense?

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.

like image 121
BalusC Avatar answered Dec 04 '25 04:12

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!