gpt4 book ai didi

java - Primefaces p :ajaxStatus onerror not called

转载 作者:行者123 更新时间:2023-11-30 07:31:56 26 4
gpt4 key购买 nike

我正在尝试在 glassfish 3.1 上使用 primefaces 2.2.1 处理 ajax 调用的 ViewExpiredException 异常。我有一个像这样的 ajaxStatus:

  <p:ajaxStatus id="ajaxStatus"  
onstart="startAjaxDisplay()"
onerror="ajaxErrorHandler()"
oncomplete="endAjaxDisplay()"/>

onstart 和 oncomplete 按预期被调用。我知道 ajaxErrorHandler() 有效,因为我把它放在 oncomplete 中,它被调用了。它现在所做的只是弹出一个 alert()。我设置了测试,服务器的响应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<partial-response>
<error>
<error-name>class javax.faces.application.ViewExpiredException</error-name>
<error-message><![CDATA[viewId:/index.xhtml - View /index.xhtml could not be restored.]]></error-message>
</error>
<changes>
<extension primefacesCallbackParam="validationFailed">{"validationFailed":false}</extension>
</changes>
</partial-response>

除了没有调用 onerror 之外,这一切都符合预期。我是否误解了它应该如何工作?

最佳答案

将不会调用 onerror 处理程序,因为 ViewExpiredException 不是 AJAX 错误,而是在构建已过期( session 已过期)的 View 期间的 JSF。PrimeFaces ajax 组件不会将这种情况作为错误处理。

在我的解决方案 (JSF2+PrimeFaces3) 中,我调查来自服务器的 ajax 响应并搜索 JSF 错误消息。请参阅下面最简单的示例:

<h:head>
<title>Facelet Title</title>
<script language="javasript" type="text/javascript">
function handleAjaxRequest(xhr, status, args){
var xmlDoc = xhr.responseXML;
errorNodes = xmlDoc.getElementsByTagName('error-name');
if (errorNodes.length == 0) return;
errorName = errorNodes[0].childNodes[0].nodeValue;
switch (errorName) {
case 'class javax.faces.application.ViewExpiredException':
alert ('Session expired, redirecting to login page!');

window.location.href = 'login.xhtml';
break;
}
}
</script>
</h:head>

<h:body>
<h:form id="frmText">
Enter the value: <p:inputText value="#{bean.text}" />
<p:commandButton value="Enter" update="frmText"
oncomplete="handleAjaxRequest(xhr, status, args);"/>
<p:separator />
The entered text is: <h:outputText value="#{bean.text}" style="font-weight: 900"/>
</h:form>
</h:body>

关于java - Primefaces p :ajaxStatus onerror not called,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6946078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com