I'm trying to handle a ViewExpiredException exception on an ajax call using primefaces 2.2.1 on glassfish 3.1. I have an ajaxStatus like this:
<p:ajaxStatus id="ajaxStatus"
onstart="startAjaxDisplay()"
开发者_C百科 onerror="ajaxErrorHandler()"
oncomplete="endAjaxDisplay()"/>
The onstart and oncomplete get called as expected. I know the ajaxErrorHandler() works because I put it in oncomplete instead and it got called. All it's doing right now is popping up an alert(). I set up my test and the response from the server looks like this:
<?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>
That's all as expected except the onerror just doesn't get called. Am I misunderstanding how this is supposed to work?
The onerror handler will be not called, because ViewExpiredException is not an AJAX error, but a JSF during the construction of the view that is already expired (the session expired). PrimeFaces ajax component not handle this situation as an error.
In my solution (JSF2+PrimeFaces3) I investigate the ajax response from the server and search for an JSF error message. See the most simple example below:
<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>
精彩评论