java JSF + Primefaces:ajax“渲染”组件的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3756256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:16:00  来源:igfitidea点击:

JSF + Primefaces: Problem with "rendered" components with ajax

javajsfprimefaces

提问by Thang Pham

EDIT
Cant seem to get renderedto work correctly with updateattributes. Here is my codes

编辑
似乎无法rendered使用update属性正常工作。这是我的代码

        <ui:define name="left">
            <h:form>
                <p:commandLink value="Hey"
                actionListener="#{bean.setRenderComment}"
                update="comment"/>
            </h:form>
       </ui:define>
       <ui:define name="right">
            <h:panelGroup id="comment" rendered="#{bean.renderComment}">
                hello
            </h:panelGroup>
       </ui:define>

renderCommentis a boolean attributes inside bean. setRenderCommentbasically toggle the state of renderCommentlike this

renderComment里面是一个布尔属性beansetRenderComment基本状态切换的renderComment这样

this.renderComment = !this.renderComment;

Right, every time I click on the link Hey, I need to refresh to either render helloon or off. How can I fix it, so that I dont need to refresh

是的,每次单击链接时Hey,我都需要刷新以hello打开或关闭渲染。我该如何修复它,这样我就不需要刷新了

回答by Romain Linsolas

I am not using Primefaces but Richfaces on my projects. So I am not really aware on how the refresh process is done by Primefaces. However, I have an idea that can be tested easily.

我在我的项目中使用的不是 Primefaces,而是 Richfaces。所以我不太清楚 Primefaces 是如何完成刷新过程的。但是,我有一个可以轻松测试的想法。

Your problem maybe due to the fact that the component to re-render (i.e. update) is not found on the HTML page. If your renderedattribute is equals to false, then the <SPAN>with commentid is notintegrated in the HTML page generated. Thus, when the Ajax request is received on the client side, the Ajax engine is not able to refresh this <SPAN>as it is not found.

您的问题可能是由于在 HTML 页面上找不到要重新呈现(即更新)的组件。如果您的rendered属性等于false,则<SPAN>with commentid集成到生成的 HTML 页面中。因此,当客户端收到 Ajax 请求时,Ajax 引擎无法刷新它,<SPAN>因为它找不到。

So what you can do is to always render your panelGroupand move your renderedattribute to a nested <h:outputText>that contains the Hellomessage.

因此,您可以做的是始终呈现您panelGrouprendered属性并将您的属性移动到<h:outputText>包含Hello消息的嵌套中。

Here is what I am suggesting:

这是我的建议:

<h:panelGroup id="comment">
    <h:outputText value="Hello" rendered="#{bean.renderComment}"/>
</h:panelGroup>

This way, the panelGroup will always be refreshed after the Ajax call, and it will contain the Hellomessage or not, regarding the value of the renderCommentattribute of your bean.

这样,在 Ajax 调用之后,panelGroup 将始终被刷新,并且它是否包含Hello消息,取决于renderComment您的 bean的属性值。

回答by BalusC

Since the component with the ID commentisn't one of the form's (an UINamingContainercomponent) children, you need to prefix the ID with :to instruct JSF to scan from the "upper level".

由于带有 ID 的组件comment不是表单(UINamingContainer组件)子项之一,因此您需要在 ID 前加上前缀:以指示 JSF 从“上层”进行扫描。

This should do:

这应该做:

<p:commandLink value="Hey"
    actionListener="#{bean.setRenderComment}"
    update=":comment" />