Ale*_*oie 11 templates facelets jsf-2
我想知道是否有可能知道是否ui:insert定义了ui:composition.我知道我可以单独使用它ui:param,但只是想这样做而不是为了保持简单并且不易出错.
示例:
模板
...
<ui:insert name="sidebar" />
<!-- Conditionnaly set the class according if sidebar is present or not -->
<div class="#{sidebar is defined ? 'with-sidebar' : 'without-sidebar'}">
<ui:insert name="page-content" />
</div>
...
Run Code Online (Sandbox Code Playgroud)
第1页
...
<ui:define name="sidebar">
sidebar content
</ui:define>
<ui:define name="page-content">
page content
</ui:define>
...
Run Code Online (Sandbox Code Playgroud)
第2页
...
<ui:define name="page-content">
page content
</ui:define>
...
Run Code Online (Sandbox Code Playgroud)
Xtr*_*ica 11
ui:param对我来说是最好的方式.这只是以正确的方式使用它的问题.举个简单的例子,我在这里定义一个参数来指定是否有侧边栏.请记住,您可以在模板中定义默认插入定义,因此只需在其中声明:
的template.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<ui:insert name="sidebar">
<!-- By default, there's no sidebar, so the param will be present.
When you replace this section for a sidebar in the client template,
the param will be removed from the view -->
<ui:param name="noSideBar" value="true" />
</ui:insert>
<div class="#{noSideBar ? 'style1' : 'style2'}">
<ui:insert name="content" />
</div>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
然后是这里的几个视图,一个使用侧边栏,另一个没有侧边栏.您可以测试它并查看样式在浏览器中的变化情况.您会注意到#{noSideBar}第二个中没有值,它将false在任何EL条件语句中进行评估.
page1.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets" template="/template.xhtml">
<ui:define name="content">
No sidebar defined? #{noSideBar}
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
page2.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets" template="/template.xhtml">
<ui:define name="sidebar" />
<ui:define name="content">
No sidebar defined? #{noSideBar}
</ui:define>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
这样您只需要担心在客户端视图中包含侧边栏或不包含侧边栏.
| 归档时间: |
|
| 查看次数: |
1428 次 |
| 最近记录: |