Primefaces Ajax oncomplete 方法
Primefaces Ajax oncomplete method
我有一个单体应用程序,其中 window.location.href 根据我的需要工作。但是现在我将我的应用程序更改为微服务架构。因此,API 在单独的服务器上是 运行,而 UI 在另一台服务器上。
现在,对于我的 xhtml 文件,我的 employeelist.xhtml
中有一个 commandButton
。
如果出现错误(API 已关闭/或任何其他错误),如何停止我的 oncomplete
进程 windows 重定向?
目前,它会记录错误,但不会显示 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 的新手,如有任何帮助,我们将不胜感激。
解决方案 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 版本,代码应该会改变,但基本思想仍然存在。
我有一个单体应用程序,其中 window.location.href 根据我的需要工作。但是现在我将我的应用程序更改为微服务架构。因此,API 在单独的服务器上是 运行,而 UI 在另一台服务器上。
现在,对于我的 xhtml 文件,我的 employeelist.xhtml
中有一个 commandButton
。
如果出现错误(API 已关闭/或任何其他错误),如何停止我的 oncomplete
进程 windows 重定向?
目前,它会记录错误,但不会显示 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 的新手,如有任何帮助,我们将不胜感激。
解决方案 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 版本,代码应该会改变,但基本思想仍然存在。