之间的主要区别<ui:include> http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/ui/include.html and <ui:decorate> http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/ui/decorate.html那是<ui:decorate>
旨在允许插入用户定义的模板组件,而<ui:include>
旨在包括现有的和已经预定义的模板。
这确实意味着<ui:decorate>
支持<ui:define> http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/ui/define.html对于用户定义的模板组件,可以将其插入到其主体中<ui:insert> http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/ui/insert.html放置在模板内。
这是一个有点笨拙的示例来展示它的用途:
/WEB-INF/templates/field.xhtml
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
<h:outputLabel for="#{id}" value="#{label}" />
<ui:insert name="input" />
<h:message id="#{id}_message" for="#{id}" />
</ui:composition>
/page.xhtml
<h:panelGrid columns="3">
<ui:decorate template="/WEB-INF/templates/field.xhtml">
<ui:param name="label" value="Foo" />
<ui:param name="id" value="foo" />
<ui:define name="input">
<h:inputText id="foo" value="#{bean.foo}" required="true" />
</ui:define>
</ui:decorate>
<ui:decorate template="/WEB-INF/templates/field.xhtml">
<ui:param name="label" value="Bar" />
<ui:param name="id" value="bar" />
<ui:define name="input">
<h:selectBooleanCheckbox id="bar" value="#{bean.bar}" required="true" />
</ui:define>
</ui:decorate>
...
</h:panelGrid>
请注意,它在面板网格的每个单元格中很好地渲染了组件。再说一遍,这个特定的例子非常笨拙,我只是使用了tag file https://stackoverflow.com/questions/5713718/how-to-make-a-grid-of-jsf-composite-component反而。仅当它是较大的部分时,例如一个完整的形式,其例如它的页眉或页脚应该是可定制的,然后<ui:decorate>
本来是合适的。
另一个主要优点<ui:decorate>
是它允许您使用带有模板的复合组件。也可以看看是否可以在 JSF 2 中使用具有复合组件的模板? https://stackoverflow.com/questions/9817851/is-it-possible-to-use-template-with-composite-component-in-jsf-2