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

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

问题描述

在处理JSF ajax请求时抛出异常时,如何处理异常并访问堆栈跟踪?现在,当JSF项目阶段设置为开发时,我只会在JavaScript警报中获取异常类名称和消息。更糟糕的是,当JSF项目阶段设置为生产时,没有任何视觉反馈,服务器日志不显示有关异常的信息。

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. Even worse, there's no visual feedback whatsoever when JSF project stage is set to Production, and the server log doesn't show any information about the exception.

如果相关,我在Netbeans中使用GlassFish。

If that's relevant, I'm using GlassFish in Netbeans.

推荐答案

p>这个问题是众所周知的,其中包括 OmniFaces FullAjaxExceptionHandler showcase

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. This example implementation merely prints the stack trace.
            exception.printStackTrace();

            // You could redirect to an error page (bad practice).
            // Or you could render a full error page (as OmniFaces does).
            // Or you could show a FATAL faces message.
            // Or you could trigger an oncomplete script.
            // etc..
        }

        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>



另请参见:



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