Skip to main content

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

4 replies [Last post]
vesuvius
Offline
Joined: 2007-06-02

Hi,

My use case is the following:

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

</p>
<p>

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:

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

So, when executing, the following error is displayed:

<br />
malformedXML: XML Parsing Error: junk after document element<br />
Location: <a href="http://localhost:8080/testApp/inventory.jsf" title="http://localhost:8080/testApp/inventory.jsf">http://localhost:8080/testApp/inventory.jsf</a><br />
Line Number 2, Column 97:<br />
<?xml version="1.0" encoding="utf-8"?><br />
<?xml version="1.0" encoding="utf-8"?><br />

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

<br />
<?xml version="1.0" encoding="utf-8"?><br />
<?xml version="1.0" encoding="utf-8"?><br />
class javax.faces.application.ViewExpiredException<br />

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

I think this is a bug. I think Mojarra must have detected that responseComplete 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.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Ryan Lubke

Hello,

Please log an issue [1] and we'll investigate.

[1] https://javaserverfaces.dev.java.net

On 2/24/10 11:07 AM, webtier@javadesktop.org wrote:
> Hi,
>
> My use case is the following:
>
> A JSF2 page has been displayed to the user. The page contains an Ajax-enabled button:
>
> [code]
>
>
>

> [/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:
>
>

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

>
class javax.faces.application.ViewExpiredException > [/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@hotmail.com)]
>
> http://forums.java.net/jive/thread.jspa?messageID=388498
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net

vesuvius
Offline
Joined: 2007-06-02

Done. Issue #1565.
In the issue, I also reported a NullPointerException thrown by Mojarra. It's related to the same problem.

(https://javaserverfaces.dev.java.net/issues/show_bug.cgi?id=1565)

roabol
Offline
Joined: 2005-01-19

Yesterday I try with Mojarra 2.0.3 (SNAPSHOT 20100302) and doesn't work, but today I was download Mojarra 2.0.3 (SNAPSHOT 20100303) and work fine.... Thanks!!!!

Message was edited by: roabol

Message was edited by: roabol

Ryan Lubke

On 3/3/10 3:37 AM, webtier@javadesktop.org wrote:
> Any solution for this case? I try with Mojarra 2.0.3 (SNAPSHOT 20100303) and doesn't work....
> [Message sent by forum member 'roabol' (roabol@yahoo.com.br)]
>
> http://forums.java.net/jive/thread.jspa?messageID=389805
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: webtier-help@glassfish.dev.java.net
>
>
Just to close the loop for those of us following this thread via email
(posts edited after their initial submission are not reflected here):
------------------------------------------------------------------------------------------------------------------------
Yesterday I try with Mojarra 2.0.3 (SNAPSHOT 20100302) and doesn't work,
but today I was download Mojarra 2.0.3 (SNAPSHOT 20100303) and work
fine.... Thanks!!!!

Message was edited by: roabol

Message was edited by: roabol
[att1.html]