我正在尝试使用 ajax 更新条件渲染的组件。
<h:form>
...
<h:commandButton value="Login" action="#{login.submit}">
<f:ajax execute="@form" render=":text" />
</h:commandButton>
</h:form>
<h:outputText id="text" value="You're logged in!" rendered="#{not empty user}" />
但是,这不起作用。我可以保证 #{user}
确实可用。这是怎么造成的以及如何解决?
请您参考如下方法:
如果组件本身没有首先渲染,则不可能通过 ajax 重新渲染(更新)组件。组件必须始终在 ajax 重新渲染之前渲染。 Ajax 使用 JavaScript document.getElementById()
来查找需要更新的组件。但是,如果 JSF 没有首先呈现该组件,那么 JavaScript 就无法找到任何要更新的内容。
解决方案是简单地引用始终呈现的父组件。
<h:form>
...
<h:commandButton ...>
<f:ajax ... render=":text" />
</h:commandButton>
</h:form>
<h:panelGroup id="text">
<h:outputText ... rendered="#{not empty user}" />
</h:panelGroup>