首页 > 解决方案 > Primefaces Ajax oncomplete 方法

问题描述

我有一个整体应用程序,其中 window.location.href 正在根据我的需要工作。但是现在我将我的应用程序更改为微服务架构。因此,API 在单独的服务器上运行,而 UI 在另一台服务器上。

现在对于我的 xhtml 文件,我commandButtonemployeelist.xhtml.

oncomplete如果出现错误(API 已关闭/或任何其他错误),我如何停止我的进程窗口重定向?

目前,它记录错误但不显示 FaceMessages,并且页面被重定向到 employee_details.xhtml 但该页面中没有详细信息,因为onEmployeeSelect 方法会引发错误。

  <p:commandButton value="#{employee.employeeId}">
    <p:ajax process="@this" listener="#{employeeBean.onEmployeeSelect(employee)}" 
   oncomplete="window.location.href='employee_details.xhtml'" />
</p:commandButton>

Backingbean 是

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());
    }

}

JSF/Java 新手,任何帮助将不胜感激。

标签: javajsfprimefaces

解决方案


解决方案 1

基本逻辑:将完整的逻辑移到您的控制器中

<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()));
    }
}

解决方案 2

基本逻辑:使用验证异常逻辑

<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();
        }
    }

解决方案 3

基本逻辑:将参数从控制器传递到页面

<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()));
    }
}

取决于 Primefaces 版本,代码应该改变,但基本思想仍然存在。


推荐阅读