【问题标题】:Primefaces Ajax oncomplete methodPrimefaces Ajax oncomplete 方法
【发布时间】:2020-09-10 06:38:30
【问题描述】:

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

现在对于我的 xhtml 文件,我的 employeelist.xhtml 中有一个 commandButton

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

目前,它会记录错误但不显示 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 新手,如有任何帮助,将不胜感激。

【问题讨论】:

  • 我会使用类似this
  • 我试图理解 link 中提供的 JSF 验证的概念。但看起来 JSF 验证发生在输入数据 from another question 或这个 link 上。但在我的情况下,错误来自支持 bean,并且 oncomplete 不听任何输入来验证。
  • 也许this 更好地解释了一种管理您的场景的方法:在您的 catch 中将一个变量放入 args 中,如那里所述,并在 oncomplete 方法中检查它,仅当它是时才执行重定向没有定义的。在该页面中还有另一种可能的解决方案,在控制器中移动完整的逻辑。
  • Primefaces 请求上下文已弃用。 link 说要使用 PrimeFaces.current().executeScript() 所以在我的场景中,catch 块返回一些随机参数。 xhtml 中的 oncomplete 有 oncomplete="resizeComplete(args.result)"resizeComplete() 是 javascript 方法。你能为我的场景提供一个示例代码吗?

标签: java jsf primefaces


【解决方案1】:

解决方案 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 的版本,代码应该有所改变,但基本思想保持不变。

【讨论】:

  • 非常感谢@WoAiNii!!!!解决方案 2 有效:) 虽然,我不得不使用 listener 而不是 action 。但这有效
猜你喜欢
  • 2013-01-23
  • 2020-07-12
  • 2017-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多