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

Pun*_*cky 1 jsf facelets

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

<ui:composition template="/templates/unauthorized.xhtml">
<ui:composition template="/templates/authorized.xhtml">
Run Code Online (Sandbox Code Playgroud)

我正在使用JSF 1.2.

DRC*_*RCB 7

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

<ui:composition template="#{loginbean.authorized ? '/templates/authorized.xhtml' : '/templates/unauthorized.xhtml'}">
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)