简体   繁体   English

Ajax 更新/渲染不适用于具有渲染属性的组件

[英]Ajax update/render does not work on a component which has rendered attribute

I'm trying to ajax-update a conditionally rendered component.我正在尝试对有条件渲染的组件进行 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}" />

However, that does not work.但是,这是行不通的。 I can assure that #{user} is actually available.我可以保证#{user}实际上是可用的。 How is this caused and how can I solve it?这是怎么引起的,我该如何解决?

It's not possible to re-render (update) a component by ajax if the component itself is not rendered in first place.如果组件本身没有首先呈现,则不可能通过 ajax 重新呈现(更新)组件。 The component must be always rendered before ajax can re-render it.在 ajax 重新渲染之前,必须始终渲染组件。 Ajax is using JavaScript document.getElementById() to find the component which needs to be updated. Ajax 使用 JavaScript document.getElementById()来查找需要更新的组件。 But if JSF hasn't rendered the component in first place, then JavaScript can't find anything to update.但是如果 JSF 没有首先呈现组件,那么 JavaScript 就找不到任何要更新的内容。

The solution is to simply reference a parent component which is always rendered.解决方案是简单地引用始终呈现的父组件。

<h:form>
    ...
    <h:commandButton ...>
        <f:ajax ... render=":text" />
    </h:commandButton>
</h:form>
<h:panelGroup id="text">
    <h:outputText ... rendered="#{not empty user}" />
</h:panelGroup>

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM