How to conditionally invoke a4j:commandButton oncomplete script depending on action outcome

Lurtz picture Lurtz · Apr 1, 2011 · Viewed 10.5k times · Source

I have a

<a4j:commandButton action="#{myBean.doCalculation}"
                   oncomplete="redirectBrowser()" />

However, if there is an error at myBean.doCalculation(), then oncomplete attribute is always invoked. How do I let it depend on result of action method?

Answer

Giorgos Dimtsas picture Giorgos Dimtsas · Apr 1, 2011

You can check for errors in the oncomplete event and redirect when none are found. It can be done like this

oncomplete="#{facesContext.maximumSeverity == null ? 'redirectBrowser()' : 'doSomethingElse()'}"