Skip to main content

Redirecting from a JSF 2.0 Ajax Request

Posted by driscoll on May 14, 2009 at 7:04 PM PDT

Somewhat recently, I had a user of JSF 2.0 ask how to do a redirect from a JSF 2.0 Ajax request. Here's one way, that I've lifted from one of our tests:


First, the bean that does the work:


@ManagedBean
@RequestScoped
public class RedirectBean {

    public String redirect() {

        FacesContext ctx = FacesContext.getCurrentInstance();

        ExternalContext extContext = ctx.getExternalContext();
        String url = extContext.encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/ajax/redirecttarget.xhtml"));
        try {

            extContext.redirect(url);
        } catch (IOException ioe) {
            throw new FacesException(ioe);

        }
        return null;
 
    }


Hopefully, I don't have to explain what this is doing - it's sending a redirect request - regardless of whether it's an Ajax request or not.


Next, the code that's calling it:



<h:head>
    <title>Ajax Redirect</title>
</h:head>
<h:body>
    <h:form id="form">
        <h:commandButton id="redirect" value="Redirect" action="#{redirectBean.redirect}">
             <f:ajax execute="@this" render="@none"/>
        </h:commandButton>
    </h:form>
</h:body>


So, what's happening here? Well, once the bean sends the redirect, the ajax client receives a message from the server telling the client to redirect to a new page - all invisibly to the user. In case you're curious, here's what's going back to the client along the wire:



"<?xml version="1.0" encoding="utf-8"?>
<partial-response>
  <redirect url="/contextpath/faces/ajax/redirecttarget.xhtml">
  </redirect>
</partial-response>"


So, a simple ajax request can redirect you to a new page. Like most of the new JSF 2 stuff, we've tried to make things "just work" as much as possible.


A little more edgecase than some of my previous blogs perhaps, but probably a useful trick to keep in mind. The code was originally written by Ryan Lubke, BTW, just to make sure that credit lands where it's due.

Now that our big push for JavaOne is wrapping up, I'm hoping to have more time to blog in the immediate future. Stay tuned.

Related Topics >>

Comments

How about add f:param inner h:commandButton

If I added f:param inner h:commandButton, then, how can I pass the param value to the redirect page?

Actually, just wrote another demo that's bundled into Mojarra's basic-ajax demo, illustrating my above comment (selectOneMenu, valueChangeListener, redirect). Thought about posting it as another blog, but since even that doesn't really represent best practice (after all, if you're going to do *nothing* but redirect, you probably shouldn't be bothering with an Ajax request), I've decided against it. But to reiterate - you can put this code into any ajax-called method, not just one with a typically navigation outcome, and it will redirect the page.

Yes, that's certainly possible. You could also use a valueChange listener operating on a Menu of pages as well - which would be a nifty demo, actually. Let me write that one. This blog was just to demonstrate the technique, not a best practice.

It would be nicer if your RedirectBean didn't have the target URL baked in. Could you use an actionListener and an f:param tag instead?

Use the navigation handler

Actually, you can rely on declarative navigation here:

<h:commandButton action="#{bean.action}" value="Invoke and redirect">
<f:ajax execute="@this"/>
</h:commandButton>

<navigation-rules>
<navigation-rule>
<from-view-id>/form.xhtml</from-view-id>
<navigation-case>
<from-action>#{bean.action}</from-action>
<from-outcome>success</from-outcome>
<to-view-id>/target.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
</navigation-rules>

@RequestScoped @ManagedBean
public class Bean {
public String action() { return "success"; }
}