如前所述,rich:panel仅在定义了facet 名称="header"时才显示标题。否则,将直接省略。
如果标题存在,其内容将被 div 包裹
<div class="rich-panel-header #{compositeComponent.attrs.headerClass}"> <composite:insertFacet name="header"/> </div>
如果上面的代码中面板在 xhtml 页面上没有标题 facet 会发生什么?PDL 处理器只在 div 中插入空内容,没有任何注释(如果 facet 名称有误也会发生同样的情况)。然而,这并不是我们需要的,因为我们希望省略整个 div。
总的来说,这看起来是一个常见的用例:如果缺失 facet,最终布局中代表该 facet 的整个块将被省略。我在 pdldocs 中没有找到类似的内容。第二个想法是使用条件操作。
以下 EL 返回 true 如果标题 facet 存在。否则,它返回 false。
#{! empty compositeComponent.facets.header}
因此,以下代码片段是一个解决方案
<c:if test="#{! empty compositeComponent.facets.header}"> <div class="rich-panel-header #{compositeComponent.attrs.headerClass}"> <composite:insertFacet name="header"/> </div> </c:if>
但是它不起作用。文章中有一个很好的解释说明为什么它不起作用
http://www.ilikespam.com/blog/c:foreach-vs-ui:repeat-in-facelets
当 c:if 测试被评估时,compositeComponent.facets总是空的。因此,即使标题存在,也不会显示标题。解决方案也在那篇文章中。我们将使用ui:fragment。最终对标题 facet 敏感的代码如下
<!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:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:c="http://java.sun.com/jstl/core" xmlns:composite="http://java.sun.com/jsf/composite"> <head> <title>panel</title> </head> <body> <composite:interface> <composite:attribute name="style" required="false"/> <composite:attribute name="styleClass" required="false"/> <composite:attribute name="headerClass" required="false"/> <composite:attribute name="bodyClass" required="false"/> </composite:interface> <composite:implementation> <h:outputStylesheet name="rich/css/panel.css" /> <div class="rich-panel #{compositeComponent.attrs.styleClass}" style="#{compositeComponent.attrs.style}"> <ui:fragment rendered="#{! empty compositeComponent.facets.header}"> <div class="rich-panel-header #{compositeComponent.attrs.headerClass}"> <composite:insertFacet name="header"/> </div> </ui:fragment> <div class="rich-panel-body #{compositeComponent.attrs.bodyClass}" > <composite:insertChildren /> </div> </div> </composite:implementation> </body> </html>更新时间:2009年1月19日
今天我们被告知,将可以在模板内使用 c:if 以及其他 jstl 类似标签进行条件操作。