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

如何在使用OmniFaces socket与JSF commandScript时屏蔽PrimeFaces ajaxStatus

手动屏蔽PrimeFaces ajaxStatus的方案

方案一:手动发起AJAX请求并设置global=false

直接改写onmessage回调函数,不依赖h:commandScript自动生成的请求逻辑,手动调用JSF AJAX API并指定global=false,从根源上阻止请求触发全局ajaxStatus:

<o:socket channel="controllerEventChannel" scope="view" onmessage="onMessage" />

<script>
function onMessage(messageData) {
    // 获取h:commandScript的实际clientId(需替换为你页面中该组件的真实ID,比如表单ID+组件ID)
    const commandScriptId = document.getElementById('onMessage').id;
    
    // 手动发起AJAX请求,强制设置global=false
    jsf.ajax.request(commandScriptId, null, {
        global: false,
        javax.faces.source: commandScriptId,
        // 将socket传递的消息数据转为请求参数,供后台通过Faces.getRequestParameter获取
        params: messageData
    });
}
</script>

<!-- 保留h:commandScript绑定后台方法,但将name设为不冲突的占位值 -->
<h:commandScript id="onMessage" name="dummyCommand" actionListener="#{backingBean.callback()}" />

方案二:在ajaxStatus中过滤特定请求来源

直接在全局p:ajaxStatus的onstart事件里判断请求来源,忽略来自目标h:commandScript的请求:

<p:ajaxStatus 
    onstart="if (PrimeFaces.ajax.Request.context.source === '你的表单ID:onMessage') { return false; }"
    <!-- 保留你原有start、stop等状态显示逻辑 -->
/>

<o:socket channel="controllerEventChannel" scope="view" onmessage="onMessage" />
<h:commandScript name="onMessage" actionListener="#{backingBean.callback()}" />

方案三:用OmniFaces Ajax回调全局修改请求配置

利用OmniFaces提供的Ajax.addCallback方法,在所有AJAX请求发送前检查来源,对目标请求强制设置global=false:

<o:socket channel="controllerEventChannel" scope="view" onmessage="onMessage" />
<h:commandScript name="onMessage" actionListener="#{backingBean.callback()}" />

<script>
document.addEventListener('DOMContentLoaded', function() {
    Ajax.addCallback({
        beforeSend: function(xhr, settings) {
            // 替换为你的h:commandScript的真实clientId
            const targetCommandId = '你的表单ID:onMessage';
            if (settings.source === targetCommandId) {
                settings.global = false;
            }
        }
    });
});
</script>

补充:实际触发ajaxStatus的是h:commandScript发起的AJAX请求,o:socket本身仅负责WebSocket消息接收,不会触发任何AJAX状态变更。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 12:37:24