异常处理在JSF的Ajax请求 [英] Exception handling in JSF ajax requests

查看:191
本文介绍了异常处理在JSF的Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何处理异常和访问堆栈跟踪时,在处理JSF Ajax请求抛出一个异常?现在,我只得到了异常类名和消息中的JavaScript警告时,JSF项目阶段设置为开发。服务器日志不显示有关异常的任何信息。如果这是相关的,我在使用NetBeans的GlassFish。

How do I handle the exception and access the stack trace when an exception is thrown while processing a JSF ajax request? Right now, I only get the exception class name and message in a JavaScript alert when JSF project stage is set to development. The server log doesn't show any information about the exception. If that's relevant, I'm using GlassFish in Netbeans.

推荐答案

这个问题是已知的,充实中除其他外的 OmniFaces FullAjaxExceptionHandler 展示

This problem is known and fleshed out in among others the OmniFaces FullAjaxExceptionHandler showcase.

基本上,你需要创建一个自定义的 的ExceptionHandler 标准JSF并不提供开箱即用(至少不是一个明智的)。其中,你将能够拿到手的异常实例造成所有的麻烦。

Basically, you need to create a custom ExceptionHandler as standard JSF doesn't provide one out the box (at least, not a sensible one). Therein you will be able to get hands on the Exception instance causing all the trouble.

下面是一个开球例如:

public class YourExceptionHandler extends ExceptionHandlerWrapper {

    private ExceptionHandler wrapped;

    public YourExceptionHandler(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public void handle() throws FacesException {
        FacesContext facesContext = FacesContext.getCurrentInstance();

        for (Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator(); iter.hasNext();) {
            Throwable exception = iter.next().getContext().getException(); // There it is!

            // Now do your thing with it. As per question's comments, you apparently
            // merely want to print the stack trace and leave the enduser behind with
            // no form of feedback. 
            exception.printStackTrace();
        }

        getWrapped().handle();
    }

    @Override
    public ExceptionHandler getWrapped() {
        return wrapped;
    }

}

为了让它运行,创建一个自定义的 ExceptionHandlerFactory 如下:

In order to get it to run, create a custom ExceptionHandlerFactory as follows:

public class YourExceptionHandlerFactory extends ExceptionHandlerFactory {

    private ExceptionHandlerFactory parent;

    public YourExceptionHandlerFactory(ExceptionHandlerFactory parent) {
        this.parent = parent;
    }

    @Override
    public ExceptionHandler getExceptionHandler() {
        return new YourExceptionHandler(parent.getExceptionHandler());
    }

}

这需要在注册faces-config.xml中如下:

<factory>
    <exception-handler-factory>com.example.YourExceptionHandlerFactory</exception-handler-factory>
</factory>

这篇关于异常处理在JSF的Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆