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="#{employee.employeeId}">
<p:ajax process="@this" listener="#{employeeBean.onEmployeeSelect(employee)}"
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.
Base logic: move oncomplete logic into your controller
<h:form>
<p:growl id="growl" />
<p:commandButton process="@this" value="#{employee.employeeId}" update="growl"
action="#{employeeBean.onEmployeeSelect(employee)}" />
</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()));
}
}
Base logic: use validation exception logic
<h:form>
<p:growl id="growl" />
<p:commandButton process="@this" value="#{employee.employeeId}" update="growl"
action="#{employeeBean.onEmployeeSelect(employee)}" 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();
}
}
Base logic: pass parameter from controller to page
<h:form>
<p:growl id="growl" />
<p:commandButton process="@this" value="#{employee.employeeId}" update="growl"
action="#{employeeBean.onEmployeeSelect(employee)}" 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With