In JSF, there are both templates and components. Component is not another word for a template.
The example you show is not a very likely candidate for a component, but is really done by a (Facelets) template:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Example</title>
<ui:include src="/resources/component/head.xhtml"/>
</h:head>
<h:body>
<ui:insert name="content"/>
<ui:include src="/resources/component/footer.xhtml"/>
</h:body>
</html>
Now individual pages can make use of this template, and are then called template clients
:
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition
xmlns:ui="http://java.sun.com/jsf/facelets"
template="/templates/master.xhtml">
<ui:define name="content">
Some content for this page.
</ui:define>
</ui:composition>
Take note of the definition of a section called content
in the template, for which the template client provides actual content.
Components
can be created via Java or via special Facelets pages called composite-components
. Composite components basically combine a number of existing components and markup to form new components that can be easily re-used. E.g. a very simple composite component without any parameters:
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite"
>
<cc:interface/>
<cc:implementation>
<p>
<h:outputText value="&nbsp;" escape="false"/>
</p>
</cc:implementation>
</ui:composition>
If you put this in [web content]/resources/foo/emptyLine.xhtml
, you can use it on your templates as <foo:emptyLine/>
. E.g.
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:m4n="http://java.sun.com/jsf/composite/foo"
template="/templates/master.xhtml">
<ui:define name="content">
Some content for this page.
<foo:emptyLine/>
</ui:define>
</ui:composition>
At times includes
and composite components
and a third variant facelets tags
can feel a bit similar to each other. In general you would use includes for things that are basically very specific for an individual page and components for things that you re-use at many locations. The difference between composite components
and facelets tags
is more subtle, but for many cases composite components
can be considered to be the more modern variant of the two.