Facelets: ui:param default value

試著忘記壹切 提交于 2019-12-22 04:32:35

问题


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?


回答1:


Could use this:

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

I hope it helps.




回答2:


A default value can be defined by using a ternary operator checking for null value.

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

This will print "Default value" if the parameter was not passed by a ui:param tag.




回答3:


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}" />


来源:https://stackoverflow.com/questions/11469750/facelets-uiparam-default-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!