How could I reuse the included jsf template that would display more/less data
For example I have main.xhtml which includes the child.xhtml
child.xhtml as below
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j" xmlns:c="http://java.sun.com/jstl/core" xmlns:s="http://jboss.com/products/seam/taglib">
<body>
<h:outputText value="One"></h:outputText>
<h:outputText value="Two"></h:outputText>
<h:outputText value="Three"></h:outputText>
<h:outputText value="Four"></h:outputText>
</body>
</html>
The current implementation of child.xhtml displays "One","Two","Three" and "Four". I want to include the above child.xhtml somewhere else where it should only display "Two" and "Four".
First of all, this is not a valid include file syntax. You're duplicating XML prolog, HTML doctype, <html>
and <body>
tags. This would only end up as illegal HTML syntax in the final output in webbrowser.
Fix it accordingly:
<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:outputText value="One" />
<h:outputText value="Two" />
<h:outputText value="Three" />
<h:outputText value="Four" />
</ui:composition>
Coming back to your concrete question, in order to conditionally render the components, just make use of rendered
attribute. You can parameterize it by passing an <ui:param>
to <ui:include>
.
E.g.
<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:outputText value="One" rendered="#{empty hide or not hide}" />
<h:outputText value="Two" />
<h:outputText value="Three" rendered="#{empty hide or not hide}" />
<h:outputText value="Four" />
</ui:composition>
which can be used as
<ui:include src="/WEB-INF/includes/client.xhtml">
<ui:param name="hide" value="#{true}" />
</ui:include>
When <ui:param name="hide">
is omitted, or its value is not true
, then they will be shown.