【问题标题】:Conditionally including a template file in ui:composition tag有条件地在 ui:composition 标记中包含模板文件
【发布时间】:2011-12-15 08:39:29
【问题描述】:

我在我的 jsf 应用程序中使用 facelets 进行模板化。我想在 ui:composition 标记中有条件地包含一个模板文件。如果用户已登录,则模板必须为“authorized.xhtml”,如果用户未登录,则模板必须为“unauthorized.xhtml”。有没有办法做到这一点?谢谢。

<ui:composition template="/templates/unauthorized.xhtml">
<ui:composition template="/templates/authorized.xhtml">

我正在使用 JSF 1.2。

【问题讨论】:

  • 我现在正在从我正在检查用户是否登录的支持 bean 中的方法中检索模板值。如果用户已登录,我返回“/templates/authorized.xhtml”,否则返回“/templates/unauthorized.xhtml”。我无法将其作为答案发布,因为我没有 100 个声誉点。

标签: jsf facelets


【解决方案1】:

如果您的登录 bean 中有一个,我会尝试对 isAuthorized() 属性进行三元运算:

<ui:composition template="#{loginbean.authorized ? '/templates/authorized.xhtml' : '/templates/unauthorized.xhtml'}">

或者使用两个带有适当rendered 值的&lt;h:panelGroup&gt; 标签:

<h:panelGroup rendered="#{loginbean.authorized}">
    <ui:decorate template="/templates/authorized.xhtml">
</h:panelGroup>

<h:panelGroup rendered="#{not loginbean.authorized}">
    <ui:decorate template="/templates/unauthorized.xhtml">
</h:panelGroup>

【讨论】:

  • 面板组方法不起作用。 &lt;ui:composition&gt; 应该是根标签。但是,EL 方法可以正常工作。
  • 你是对的。必须使用&lt;ui:decorate&gt; 而不是&lt;ui:composition&gt;
猜你喜欢
  • 2023-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
  • 2016-04-05
相关资源
最近更新 更多