How to redirect from a ManagedBean for when the request sent is an Ajax request?

Bhesh Gurung picture Bhesh Gurung · May 17, 2011 · Viewed 41.8k times · Source

I am using PrimeFaces with JSF2. I am trying to authenticate user by sending login and password as an Ajax request. And in the action method of the backing bean, I am trying to validate user and redirect to a new view if the validation succeeds.

Is this possible while using primefaces?

Because I think with primefaces' p:commandButton, I can only either have ajax behavior or the navigation.

Answer

BalusC picture BalusC · May 17, 2011

Yes, just send a redirect instead of a (default) forward as outcome. The <navigation-case>-less JSF 2.0 way would be appending ?faces-redirect=true to the outcome string in the action method.

E.g.

public String login() {
    // ...
    return "home?faces-redirect=true";
}