3

Here's my scenario: I'd like to update a page via Ajax in some cases, in other cases, execute a navigation rule. My use case is a login form. I'd like them to receive an error message via ajax if their uname/password fails, but navigate to a new page if it succeeds.

Has anyone done this using JSF2.0 f:ajax apis? I'm not really interested in solutions that go outside standard facelets, jsf2.0, etc.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84

1 Answers1

4

It's not different from when doing it without ajax. Just return the next view ID as String the usual way via <h:commandXxx action> (and thus not <f:ajax listener>).

So, just

<h:commandButton value="Login" action="#{bean.login}">
    <f:ajax execute="@form" render="@form" />
</h:commandButton>

with

public String login() {
    // ...

    return "nextpage";
}

will work as good as without <f:ajax>. It'll just go to nextpage.xhtml.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Neat! if you want to stay on the 'current page', you just return "" I discovered. Wow, this was not difficult at all! – Jonathan S. Fisher Feb 03 '12 at 04:53
  • You're welcome. When you want to navigate back to the same view, rather return `null` (or `void` if possible) instead of empty string so that the view (and the view scoped bean, if any) will be reused instead of recreated. – BalusC Feb 03 '12 at 04:54