You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Primefaces关闭NotificationBar后彻底移除不重复渲染问题求助

问题原因

你当前的实现失效的核心原因是:update属性指定的目标组件id-announcement本身绑定了rendered条件,当你执行完announcementClosed方法后,shouldDisplayAnnouncement()已经返回false,此时该组件已经不在JSF的组件渲染树中,AJAX更新流程找不到对应目标,自然无法生效,最终只有组件自带的客户端隐藏逻辑执行了,服务端的渲染条件没有触发重计算。

解决办法

你只需要调整update的目标为一个始终会被渲染的父级容器即可,最简单的方案是直接更新外层的announcement-form表单,表单作为父容器始终存在,更新时会自动重新计算内部所有子组件的rendered条件。
另外建议给p:commandLink添加process="@this"属性,避免不必要的全表单数据提交,仅处理当前关闭按钮的请求即可。

修正后代码示例

<h:form id="announcement-form">
    <p:notificationBar id="id-announcement"
            position="top" effect="slide"
            autoDisplay="true" styleClass="top"
            widgetVar="announcement"
            rendered="#{announcementSupport.shouldDisplayAnnouncement()}">
        <p:commandLink title="Close" class="ui-notificationbar-close"
                        action="#{announcementSupport.announcementClosed}"
                        process="@this"
                        update=":announcement-form">
            <i class="fa fa-times fa-2x" />
        </p:commandLink>
        <div class="announcement">
            <div class="logo">
            </div>
            <div class="content">
                <div class="title">
                    <h:outputText value="#{messages['main.announcement.title']}"/>
                </div>
                <div class="body">
                    <h:outputText value="#{announcementSupport.announcement}"/>
                </div>
            </div>
        </div>
    </p:notificationBar>
</h:form>

额外注意事项

请确保announcementSupport Bean的作用域合适(比如至少是@ViewScoped或者@SessionScoped),且announcementClosed方法执行后,shouldDisplayAnnouncement()的返回值会持久化标记为false(比如把用户已关闭公告的标记存在会话中),避免用户刷新页面后公告再次出现。

内容的提问来源于stack exchange,提问作者Olgun Kaya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 23:54:05