在ui:composition标签中有条件地包含模板文件

4

我在我的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个声望点,我无法将其发布为答案。 - Punter Vicky
1个回答

10

如果您的登录bean中有isAuthorized()属性,我建议尝试使用三元运算符操作:

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

或者使用两个具有适当rendered值的<h:panelGroup>标签:

<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>

1
面板组(panelgroup)方法行不通。<ui:composition> 应该是根标签。但 EL 方法可以正常工作。 - BalusC
1
你是对的。必须使用<ui:decorate>而不是<ui:composition> - DRCB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接