java - Primefaces Ajax oncomplete 方法
问题描述
我有一个整体应用程序,其中 window.location.href 正在根据我的需要工作。但是现在我将我的应用程序更改为微服务架构。因此,API 在单独的服务器上运行,而 UI 在另一台服务器上。
现在对于我的 xhtml 文件,我commandButton
的employeelist.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 新手,任何帮助将不胜感激。
解决方案
解决方案 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 版本,代码应该改变,但基本思想仍然存在。
推荐阅读
- ckeditor - 如何在 CKEditor 5 Classic 中使用 url 图片插入对话框而不是上传
- excel - 如何在 VBA 的单元格中输入公式的值?
- javascript - Firebase 实时数据库 + JS 对象
- java - Deltaspike TransactionStrategy Alternative 模糊依赖
- python - 在 python 字典中转换 2 个列表
- javascript - 使用 Mailchimp 的 API 将订阅者添加到列表
- python - 如何在一个序列化程序类中合并 django rest 框架中的模型
- oracle - 在 PL/SQL 中检查日期
- javascript - 反应本机错误!元素类型无效:应为字符串或类/函数,但得到:未定义
- javascript - 如何根据其中存在的图像动态增加 div 高度?