Richfaces 4.5 中 onError 属性的使用
Usage of onError attribute in Richfaces 4.5
我想在 Richfaces 中使用 onerror 属性来处理我的 ajax 请求的异常。为此,我使用了
<a4j:commandButton value="OK"
actionListener="#{aBean.handler()}"
onerror="showNotification();">
在我的托管 bean 中:
ABean{
public void handler(){
throw new SomeException("Error to the browser");
}
}
尽管我在我的处理程序中抛出了异常,但我的 showNotification() 从未被调用过。
我可以使用 onerror 属性处理我的应用程序级异常吗?非常感谢有关此主题的任何指示或示例。
在 docs 中您可以看到 onerror
属性有效:
when the request results in an error
这基本上意味着请求必须以 HTTP 错误结束。例如 HTTP 500,这可能意味着服务器目前不可用。
例子 (java):
public void handler2() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().responseSendError(HttpServletResponse.SC_NOT_FOUND,
"404 Page not found!");
context.responseComplete();
}
和a4j:commandButton
(xhtml)
<a4j:commandButton value="OK" actionListener="#{bean.handler2}"
onerror="console.log('HTTP error');" />
在 JavaScript 控制台中你应该看到 "HTTP error".
在任何其他异常情况下 oncomplete
代码将被触发,因为 AJAX 请求以成功结束。因此,如果您不想对代码中的异常做出反应,则必须自己处理。有很多方法可以做到这一点。我用这个:
public boolean isNoErrorsOccured() {
FacesContext facesContext = FacesContext.getCurrentInstance();
return ((facesContext.getMaximumSeverity() == null) ||
(facesContext.getMaximumSeverity()
.compareTo(FacesMessage.SEVERITY_INFO) <= 0));
}
我的 oncomplete
看起来像这样:
<a4j:commandButton value="OK" execute="@this" actionListener="#{bean.handler1}"
oncomplete="if (#{facesHelper.noErrorsOccured})
{ console.log('success'); } else { console.log('success and error') }" />
并使用这样的处理程序:
public void handler1() {
throw new RuntimeException("Error to the browser");
}
在 JavaScript 控制台中你会看到 "success and error".
顺便说一句。最好写actionListener="#{bean.handler3}"
而不是actionListener="#{bean.handler3()}"
。背后的原因:
public void handler3() { // will work
throw new RuntimeException("Error to the browser");
}
// the "real" actionListener with ActionEvent won't work and
// method not found exception will be thrown
public void handler3(ActionEvent e) {
throw new RuntimeException("Error to the browser");
}
我想在 Richfaces 中使用 onerror 属性来处理我的 ajax 请求的异常。为此,我使用了
<a4j:commandButton value="OK"
actionListener="#{aBean.handler()}"
onerror="showNotification();">
在我的托管 bean 中:
ABean{
public void handler(){
throw new SomeException("Error to the browser");
}
}
尽管我在我的处理程序中抛出了异常,但我的 showNotification() 从未被调用过。
我可以使用 onerror 属性处理我的应用程序级异常吗?非常感谢有关此主题的任何指示或示例。
在 docs 中您可以看到 onerror
属性有效:
when the request results in an error
这基本上意味着请求必须以 HTTP 错误结束。例如 HTTP 500,这可能意味着服务器目前不可用。
例子 (java):
public void handler2() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
context.getExternalContext().responseSendError(HttpServletResponse.SC_NOT_FOUND,
"404 Page not found!");
context.responseComplete();
}
和a4j:commandButton
(xhtml)
<a4j:commandButton value="OK" actionListener="#{bean.handler2}"
onerror="console.log('HTTP error');" />
在 JavaScript 控制台中你应该看到 "HTTP error".
在任何其他异常情况下 oncomplete
代码将被触发,因为 AJAX 请求以成功结束。因此,如果您不想对代码中的异常做出反应,则必须自己处理。有很多方法可以做到这一点。我用这个:
public boolean isNoErrorsOccured() {
FacesContext facesContext = FacesContext.getCurrentInstance();
return ((facesContext.getMaximumSeverity() == null) ||
(facesContext.getMaximumSeverity()
.compareTo(FacesMessage.SEVERITY_INFO) <= 0));
}
我的 oncomplete
看起来像这样:
<a4j:commandButton value="OK" execute="@this" actionListener="#{bean.handler1}"
oncomplete="if (#{facesHelper.noErrorsOccured})
{ console.log('success'); } else { console.log('success and error') }" />
并使用这样的处理程序:
public void handler1() {
throw new RuntimeException("Error to the browser");
}
在 JavaScript 控制台中你会看到 "success and error".
顺便说一句。最好写actionListener="#{bean.handler3}"
而不是actionListener="#{bean.handler3()}"
。背后的原因:
public void handler3() { // will work
throw new RuntimeException("Error to the browser");
}
// the "real" actionListener with ActionEvent won't work and
// method not found exception will be thrown
public void handler3(ActionEvent e) {
throw new RuntimeException("Error to the browser");
}