Published 22 Oct, 2022

Java - Primefaces Ajax oncomplete method

Category Java
Modified : Nov 30, 2022
24

I had a monolithic application, in which window.location.href was working as according to what I needed. But now I changed my application to microservices architecture. So, API is running on a separate server while UI is on another server.

Now for my xhtml file, I have a commandButton in my employeelist.xhtml.

How do I stop my oncomplete process windows redirect if there is an error (API is down / or any other errors)?

Currently, it logs the errors but FaceMessages are not displayed and also, the page is redirected to employee_details.xhtml but there are no details in that page as onEmployeeSelect method throws an error.

  <p:commandButton value="#">
    <p:ajax process="@this" listener="#" 
   oncomplete="window.location.href='employee_details.xhtml'" />
</p:commandButton>

Backingbean is

public void onEmployeeSelect(EmployeeDefinition employeeVO) {
   try{
         //calls API
        if (success) {
          //show employee details
        }
    }
    catch(Exception ex){
       //if API is not reachable
        addMessage(FacesMessage.SEVERITY_ERROR, ERROR, ex.getMessage());
    }

}

New to JSF/Java, any help would be appreciated.

Answers

There are 1 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Java, Primefaces, Jsf. These have been categorized in sections for a clear and precise explanation.

44

Solution 1:

Base logic: move oncomplete logic into your controller

<h:form>
    <p:growl id="growl" />
    <p:commandButton process="@this" value="#" update="growl"
        action="#" />
</h:form>

...

public void onEmployeeSelect(EmployeeDefinition employeeVO) {
    try {
        // calls API
        PrimeFaces.current().executeScript("window.location.href='employee_details.xhtml'");
    } catch (Exception ex) {
        // if API is not reachable
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", ex.getMessage()));
    }
}

Solution 2:

Base logic: use validation exception logic

<h:form>
    <p:growl id="growl" />
    <p:commandButton process="@this" value="#" update="growl"
        action="#" oncomplete="if (!args.validationFailed) window.location.href='employee_details.xhtml'" />
</h:form>

...

public void onEmployeeSelect(EmployeeDefinition employeeVO) {
        try {
            // calls API
//          int i=1/0;

        } catch (Exception ex) {
            // if API is not reachable
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", ex.getMessage()));
            FacesContext.getCurrentInstance().validationFailed();
        }
    }

Solution 3:

Base logic: pass parameter from controller to page

<h:form>
    <p:growl id="growl" />
    <p:commandButton process="@this" value="#" update="growl"
        action="#" oncomplete="if (args.rhonda) window.location.href='employee_details.xhtml'" />
</h:form>

...

public void onEmployeeSelect(EmployeeDefinition employeeVO) {
    try {
        // calls API
        int i=1/0;

        PrimeFaces.current().ajax().addCallbackParam("rhonda", "rhonda");

    } catch (Exception ex) {
        // if API is not reachable
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", ex.getMessage()));
    }
}

Depends on Primefaces version, the code should change, but the base idea remains.