Java 素数法

Java 素数法,java,jsf,primefaces,Java,Jsf,Primefaces,我有一个单片应用程序,其中window.location.href根据我的需要工作。但是现在我把我的应用程序改成了微服务架构。所以,API在单独的服务器上运行,而UI在另一台服务器上运行 现在,对于我的xhtml文件,我在employeelist.xhtml中有一个commandButton 如果出现错误(API关闭/或任何其他错误),如何停止我的oncomplete进程窗口重定向 目前,它会记录错误,但不会显示FaceMessages,而且该页面会重定向到employee_details.xh

我有一个单片应用程序,其中window.location.href根据我的需要工作。但是现在我把我的应用程序改成了微服务架构。所以,API在单独的服务器上运行,而UI在另一台服务器上运行

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

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

目前,它会记录错误,但不会显示FaceMessages,而且该页面会重定向到employee_details.xhtml,但该页面中没有详细信息,因为
OneEmployeeSelect
方法会抛出错误

  <p:commandButton value="#{employee.employeeId}">
    <p:ajax process="@this" listener="#{employeeBean.onEmployeeSelect(employee)}" 
   oncomplete="window.location.href='employee_details.xhtml'" />
</p:commandButton>
JSF/Java新手,如有任何帮助,将不胜感激。

解决方案1: 基本逻辑:将oncomplete逻辑移到控制器中

<h:form>
    <p:growl id="growl" />
    <p:commandButton process="@this" value="#{employee.employeeId}" update="growl"
        action="#{employeeBean.onEmployeeSelect(employee)}" />
</h:form>
解决方案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>
解决方案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>

根据Primefaces的版本,代码应该会更改,但基本思想仍然存在。

我会使用一些类似于我试图理解中提供的JSF验证概念的东西。但JSF验证似乎发生在输入数据或其他数据上。但在我的例子中,错误来自于支持bean,oncomplete不监听任何输入以进行验证。也许可以更好地解释一种管理场景的方法:在catch中,将一个变量放入args,如前所述,并在oncomplete方法中检查它,仅在未定义重定向的情况下执行重定向。在该页面中还有另一种可能的解决方案,即在控制器中移动oncomplete逻辑。Primefaces请求上下文已被弃用。表示使用
PrimeFaces.current().executeScript()
,因此在我的场景中,catch块返回一些随机参数。xhtml中的oncomplete具有
oncomplete=“resizeComplete(args.result)”
resizeComplete()
是javascript方法。你能为我的场景提供一个示例代码吗?非常感谢@WoAiNii!!!!解决方案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();
        }
    }
<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()));
    }
}