<a jsf:rendered="#{...}"> 未被解释为直通元素

<a jsf:rendered="#{...}"> is not interpreted as passthrough element

我不明白为什么这段代码有效:

<h:link value="Login" rendered="#{sessionBean.userInSessionBean == null}"  />

这段代码不起作用:

<a jsf:rendered="#{sessionBean.userInSessionBean == null}">Login</a>

一个 HTML 元素只有在满足以下条件时才会成为直通元素:

  1. http://xmlns.jcp.org/jsf 命名空间中至少有一个 jsf:xxx attribute
  2. 至少有一个“identifying attribute”与特定的 JSF 组件相关联。

对于 <a> 元素,标识属性是必需的,因此 JSF 可以决定是将其解释为 <h:commandLink><h:outputLink> 还是 <h:link>。如果没有标识属性,JSF 将不知道您实际打算使用什么组件,因此任何 jsf:xxx 属性都将被忽略。 jsf:rendered 不足以作为标识属性,因为它出现在每个单独的 JSF 组件上,因此 JSF 仍然不知道您指的是哪一个。

鉴于您似乎打算拥有 <h:link>,则使用 jsf:outcome 作为标识属性。

<a jsf:outcome="login" jsf:rendered="#{empty sessionBean.userInSessionBean}">Login</a>

一个完全不同的替代方法是将普通 HTML 包装在 <ui:fragment rendered> 中。另见 How to conditionally render plain HTML elements like <div>s?