1
votes

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?

3

3 Answers

4
votes

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()'}"
3
votes

If you're on JSF 2.0, you can use FacesContext#isValidationFalied() to check if validation has failed or not:

oncomplete="if (#{not facesContext.validationFailed}) redirectBrowser()"

However, just performing redirect in the action method is more clean:

public String doCalculation() {
    // ...

    if (success) {
        return "nextPage.xhtml?faces-redirect=true";
    } else {
        return null;
    }
}

Yet more clean would be to just do the validation by a Validator. This way the action method will simply not be invoked at all when the validation has failed.

2
votes

Well, just looking for facesContext.maximumSeverity == null is not enough, there may be a warning as current max severity, that should redirect, too. Instead, write a method that looks if there are any max severities of priority error...