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

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


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";}


Here is another technique you might find useful. This is when you invoke method via AJAX from a Primefaces attribute that does not implement navigation. For example, I have a p:tree object with a method selected by the nodeSelectionListener.

In that method, you can invoke redirection like this:

String url = (something)FacesContext fc = FacesContext.getCurrentInstance();ExternalContext ec = fc.getExternalContext();try {        ec.redirect(url);} catch (IOException ex) {        Logger.getLogger(Navigation.class.getName()).log(Level.SEVERE, null, ex);}

Hope you find this useful.