jsffaceletsjsf-2.2tagfile

How do I include a body into a tagfile


I have a tagfile I intend to use as an input template:

<ui:composition
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">

    <div class="qr">
        <label>#{question}</label>
        <div class="helpButton"></div>
        <!-- body here -->
        <!-- errors output eventually to go here -->
    </div>

</ui:composition>

It is stored in my /WEB-INF/tags folder with a .taglib.xml and necessary web.xml context-param.

I understand it could be used as follows:

<g:question question="What is your name?" id="firstname">
    <h:inputText value="#{bean.firstname}" />
</g:question>

at the moment this is in its most basic form. I intend to use various and complex inputs. but the layout of the label etc will always need to stay the same.

How do I include the body of the <g:question> in the tagfile?


Solution

  • Use <ui:insert>.

    <ui:composition
        xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:ui="http://java.sun.com/jsf/facelets">
    
        <div class="qr">
            <label>#{question}</label>
            <div class="helpButton"></div>
            <ui:insert />
            <!-- errors output eventually to go here -->
        </div>
    
    </ui:composition>
    

    Do note that you can even use <ui:define name="..."> and <ui:insert name="..."> on tagfiles like as you usually do on templates.