It really won't work. It is assumed that <ui:define> used in the template client (i.e., the page with <ui:composition template="..."> ), and not in the include file (that is, the page with <ui:composition> without template ) However, you can simply โextendโ existing templates.
Remove from custom.xhtml :
<ui:include src="common.xhtml" />
Edit common.xhtml
template="custom.xhtml"
And open common.xhtml instead of custom.xhtml in a browser.
See also:
- How to include another XHTML in XHTML using JSF 2.0 Facelets?
Unrelated to a specific problem, in order to prevent the enduser form from opening custom.xhtml or template.xhtml directly in the browser, it is recommended that you move them to the /WEB-INF folder. Also, do you know about the <h:head> and <h:outputStylesheet> ? I suggest using them. Also, having <h1> end up in <head> doesn't make sense. Did you mean that <ui:insert name="header"> is inside the <body> ? In addition, you can easily place this <h1> in the template so that you do not need to repeat them in each client of the template.
/WEB-INF/templates/template.xhtml
<html ...> <h:head> </h:head> <h:body> <ui:insert name="header" /> <ui:insert name="body" /> </body> </html>
/WEB-INF/templates/custom.xhtml (the CSS file is placed in the /resources folder)
<ui:composition ... template="/WEB-INF/templates/template.xhtml"> <ui:define name="header"> <h1><ui:insert name="custom-header" /></h1> </ui:define> <ui:define name="body"> <h:outputStylesheet name="custom.css" target="head" /> <ui:insert name="custom-body" /> </ui:define> </ui:composition>
/page.xhtml
<ui:composition ... template="/WEB-INF/templates/custom.xhtml"> <ui:define name="custom-header"> header </ui:define> <ui:define name="custom-body"> <table><tr><td>Table</td></tr></table> </ui:define> </ui:composition>
See also:
- Which XHTML files do I need to enter / WEB -INF and which do not?
- How to link to CSS / JS / image resource in Facelets template?
Balusc
source share