4

On p:ajax call,listener invokes method which contains

     FacesContext.getCurrentInstance().getExternalContext().dispatch("/uri.jsf");  

doesn't work. Ive set a break point on the line and it remains at the same point on execution.It doesn't move forward, I ve got to restart server to run the application again.

    FacesContext.getCurrentInstance().getExternalContext().redirect("/uri.jsf");

redirection works perfectly fine. But i want page forward which is dispatch to navigate to another page.

shashdr
  • 83
  • 2
  • 8

1 Answers1

5

The ExternalContext#dispatch() does not support ajax requests. It causes JSF to render the HTML output of the given resource which can't be understood by the JavaScript ajax engine. The ajax request has to return a special XML response which can be understood by the JavaScript ajax engine.

The ExternalContext#redirect() supports ajax requests. It will automatically return a special XML response instructing the JavaScript ajax engine to invoke a window.location call on the given URL (you can find an XML example in this answer).

You have 2 options:

  1. Make it a non-ajax request.
  2. Perform a normal JSF navigation.

Making a non-ajax request is most likely not an option for <p:ajax>. In that case, performing a normal navigation is really your only option.

FacesContext context = FacesContext.getCurrentInstance();
context.getApplication().getNavigationHandler().handleNavigation(context, null, "/uri.jsf");

It will in case of ajax requests automatically force an render="@all" with the new content.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I am using normal Jsf navigation now. It navigates to new page . Now if i perform some event on (clicking button or link) the new page, it call intialize method and rendering the page again. Second time If i perform the event again , then the actual method is getting called and everything works normal. – shashdr Jan 04 '13 at 20:21
  • This is recognizable as the missing view state bug (however, I wouldn't expect it to occur on ``, but only on ``, did you really use ``?). You can fix it by the following answer: http://stackoverflow.com/questions/11408130/jsf-commandbutton-works-on-second-click/11412138#11412138 – BalusC Jan 04 '13 at 20:24
  • OK, what browser are you using? MSIE? If so, head to this answer instead then: http://stackoverflow.com/questions/13552531/jsf-language-switcher-and-ajax-update/13572683#13572683 – BalusC Jan 04 '13 at 20:30
  • Nope Firefox and chrome, haven't yet tested it on IE – shashdr Jan 04 '13 at 20:32
  • Ok let me explain my issue, I am using p:selectonemenu where on clicking each item label should navigate to a new page. I used p:ajax to act as action to invoke methods of each item lable that navigate to their respective pages. externalcontext.redirect is working perfect but I had this issue( http://stackoverflow.com/questions/14146398/primefaces-ajax-status-stops-on-redirection-to-new-page). And on Page forward the issue in link gets resolved. – shashdr Jan 04 '13 at 20:43
  • Well, I would rather perform the navigation synchronously instead of asynchronously. See also bottommost example of http://stackoverflow.com/questions/1821138/how-to-reload-a-jsf-page-from-a-valuechangeevent/1821708#1821708 – BalusC Jan 04 '13 at 20:46