简体   繁体   English

Richfaces 4.5中onError属性的用法

[英]Usage of onError attribute in Richfaces 4.5

I want to make use of onerror attribute in Richfaces to handle exceptions for my ajax requests. 我想利用Richfaces中的onerror属性来处理我的Ajax请求的异常。 For that i have used 为此,我用了

<a4j:commandButton value="OK" 
actionListener="#{aBean.handler()}"
onerror="showNotification();">

and in my Managed bean : 在我的Managed bean中:

ABean{
    public void handler(){
        throw new SomeException("Error to the browser");
    }
}

Though, i have thrown exception in my handler, my showNotification() is never called. 虽然,我在处理程序中引发了异常,但从未调用过我的showNotification()。

Can I handle my application level exceptions using onerror attribute? 我可以使用onerror属性处理应用程序级别的异常吗? Any pointers or examples on this topic is much appreciated. 对此主题的任何指示或示例均深表感谢。

In docs you can read that onerror attribute works: 文档中,您可以阅读onerror属性的工作原理:

when the request results in an error 当请求导致错误时

This basically means that request must ends with HTTP error . 这基本上意味着请求必须以HTTP错误结束 For example HTTP 500, which can mean that server is not avaible at the moment. 例如HTTP 500,这可能意味着服务器当前不可用。

Example of it (java): 示例(java):

public void handler2() throws IOException {
    FacesContext context = FacesContext.getCurrentInstance();
    context.getExternalContext().responseSendError(HttpServletResponse.SC_NOT_FOUND,
            "404 Page not found!");
    context.responseComplete();
}

and a4j:commandButton (xhtml) a4j:commandButton (xhtml)

<a4j:commandButton value="OK" actionListener="#{bean.handler2}" 
    onerror="console.log('HTTP error');" />

In JavaScript console you should see "HTTP error". 在JavaScript控制台中,您应该看到“ HTTP错误”。

In any other case of exceptions oncomplete code will be fired, since AJAX request end with success. 在其他任何异常情况下,由于AJAX请求成功结束,因此将触发oncomplete代码。 So if you wan't to react to the exception in your code you must handle it yourself. 因此,如果您不想对代码中的异常做出反应,则必须自己处理它。 There are many ways to do this. 有很多方法可以做到这一点。 I use this: 我用这个:

public boolean isNoErrorsOccured() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    return ((facesContext.getMaximumSeverity() == null) || 
                (facesContext.getMaximumSeverity()
                    .compareTo(FacesMessage.SEVERITY_INFO) <= 0));
}

And my oncomplete looks like this: 而我oncomplete看起来是这样的:

<a4j:commandButton value="OK" execute="@this" actionListener="#{bean.handler1}"
    oncomplete="if (#{facesHelper.noErrorsOccured})
        { console.log('success'); } else { console.log('success and error') }" />

and with handler like this: 和这样的处理程序:

public void handler1() {
    throw new RuntimeException("Error to the browser");
}

In JavaScript console you would see "success and error". 在JavaScript控制台中,您会看到“成功与错误”。


BTW. 顺便说一句。 It's better to write actionListener="#{bean.handler3}" instead of actionListener="#{bean.handler3()}" . 最好编写actionListener="#{bean.handler3}"而不是actionListener="#{bean.handler3()}" The reason behind it: 其背后的原因:

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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM