Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facelets: ui:param default value

Tags:

jsf

facelets

How can one define a default value for a facelet template parameter? Consider the following element using a template parameter:

<h:outputText value="#{templParam}"></h:outputText>

The above line will print the the template parameter templParam which is passed by a ui:param tag in a ui:composition using the template:

<ui:param name="templParam" value="Hello world"></ui:param>

But if ui:param tag is missing nothing will be printed. Although, how can one print e.g "Default value" in this case?

like image 505
Sebastian Hoffmann Avatar asked Jul 13 '12 11:07

Sebastian Hoffmann


2 Answers

Could use this:

<h:outputText value="#{empty templParam ? 'Default value' : templParam}" />

I hope it helps.

like image 130
rbento Avatar answered Oct 18 '22 03:10

rbento


After the composition tag to define the start of the template, the template parameter can be set to its default value (if it is empty) so that all following uses of it don't require checking for a null each time (and its default value is in one place in the code).

<html xmlns:c="http://java.sun.com/jsp/jstl/core" >

<ui:composition>
    <c:set var="templParam" value="#{empty templParam ? 'Default value' : templParam}"
           scope="request" />
    <h:outputText value="Use 1: #{templParam}" />
    <h:outputText value="Use 2: #{templParam}" />
like image 44
Martin Avatar answered Oct 18 '22 03:10

Martin