webtier@glassfish.java.net

JSF 2: Ajax redirection from inside a PhaseListener (possibly a bug!)

From: <webtier_at_javadesktop.org>
Date: Wed, 24 Feb 2010 11:07:45 PST

Hi,

My use case is the following:

A JSF2 page has been displayed to the user. The page contains an Ajax-enabled button:

[code]
<h:commandButton ...>
    <f:ajax execute="..." render="..."/>
</h:commandButton>
[/code]

So, the user waits for a LONG time while the page is on the screen and the session expires. After that, the user presses the Ajax-enabled button.

What I want to happen is: redirect the user to the login screen.

So, following Jim Driscoll's advice from http://weblogs.java.net/blog/2009/05/14/redirecting-jsf-20-ajax-request, I created a PhaseListener which checks whether the session has expired and, if so, redirects to the login page:

[code]
ExternalContext ectx = ctx.getExternalContext();
String newUrl = ectx.encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/login.xhtml"));
try {
    ectx.redirect(newUrl);
    //ctx.renderResponse();
} catch (IOException ex) {
    throw new FacesException(ex);
}
[/code]

So, when executing, the following error is displayed:

[code]
malformedXML: XML Parsing Error: junk after document element
Location: http://localhost:8080/testApp/inventory.jsf
Line Number 2, Column 97:
<?xml version="1.0" encoding="utf-8"?>
<partial-response><redirect url="/testApp/inventory.jsf"></redirect></partial-response><?xml version="1.0" encoding="utf-8"?>
[/code]

The full response that was sent by the server (Glassfish v3 that came with NetBeans IDE 6.8 (Build 200912041610)) is:

[code]
<?xml version="1.0" encoding="utf-8"?>
<partial-response><redirect url="/testApp/inventory.jsf"></redirect></partial-response><?xml version="1.0" encoding="utf-8"?>
<partial-response><error><error-name>class javax.faces.application.ViewExpiredException</error-name><error-message><![CDATA[viewId:/inventory.jsf - View /inventory.jsf could not be restored.viewId:/inventory.jsf - View /inventory.jsf could not be restored.]]></error-message></error></partial-response>
[/code]

As you can see, [b]along with the redirection, Mojarra has attached ANOTHER XML containing an error message: javax.faces.application.ViewExpiredException...[/b].

I think this is a bug. I think Mojarra must have detected that [b]responseComplete[/b] had already been called, and to NOT send the additional stuff.

Please, give me advice what to do, so that I can redirect successfully to my login screen.
[Message sent by forum member 'vesuvius' (vesuvius_prime_at_hotmail.com)]

http://forums.java.net/jive/thread.jspa?messageID=388498