沉默FullAjaxExceptionHandler

时间:2013-08-01 19:54:10

标签: jsf-2 glassfish omnifaces

因此,在面对可怕的javax.faces.application.ViewExpiredException后,我不得不环顾互联网寻找合适的解决方案。幸运的是,解决方案随时可用,我继续采用OmniFaces FullAjaxExceptionHandler

足够说,就像OmniFaces的几乎所有东西一样,它创造了奇迹。但是,每当我有一个视图到期时,我都会得到:

SEVERE: WebModule[/myModule]FullAjaxExceptionHandler: An exception occurred during processing JSF ajax request. Error page '/WEB-INF/errorpages/test.xhtml' will be shown.
javax.faces.application.ViewExpiredException: viewId:/my/page.xhtml - View /my/page.xhtml could not be restored.
...

这很好,因为它是按预期处理的,但是无论如何都要将此异常打印到server.log ?这会快速占用日志。

我正在跑步:
Mojarra 2.1.23
PrimeFaces 4.0-SNAPSHOT
OmniFaces 1.6-SNAPSHOT-2013-07-01


Glassfish 3.1.2.2

1 个答案:

答案 0 :(得分:6)

根据OmniFaces 1.6,您可以扩展它并覆盖方法logException(),如下所示跳过ViewExpiredException的堆栈跟踪。

public class YourAjaxExceptionHandler extends FullAjaxExceptionHandler {

    public YourAjaxExceptionHandler(ExceptionHandler wrapped) {
        super(wrapped);
    }

    @Override
    protected void logException(FacesContext context, Throwable exception, String location, String message, Object... parameters) {
        if (exception instanceof ViewExpiredException) {
            // With exception==null, no trace will be logged.
            super.logException(context, null, location, message, parameters);
        }
        else {
            super.logException(context, exception, location, message, parameters);
        }
    }

}

围绕它创建一个工厂:

public class YourAjaxExceptionHandlerFactory extends ExceptionHandlerFactory {

    private ExceptionHandlerFactory wrapped;

    public YourAjaxExceptionHandlerFactory(ExceptionHandlerFactory wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public ExceptionHandler getExceptionHandler() {
        return new YourAjaxExceptionHandler(getWrapped().getExceptionHandler());
    }

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

}

为了让它运行,请以通常的方式在faces-config.xml中将其注册为工厂(不要忘记删除FullAjaxExceptionHandlerFactory的原始注册):

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