【问题标题】:JSF 2 : Is this a good approach to handle Business exceptions?JSF 2:这是处理业务异常的好方法吗?
【发布时间】:2010-11-26 07:23:54
【问题描述】:

根据我在 Creating FacesMessage in action method outside JSF conversion/validation mechanism? 中的上一个问题,我正在尝试处理从我的托管 bean 之外的业务层抛出的异常。

策略是在 PhaseListener 中搜索业务异常并将其转换为 Faces 消息。

它按我的预期工作,但我只是想知道我只是重新发明轮子,还是用错误的方式做对了?

这是我的代码示例 sn-p:

public class BusinessExceptionHandler implements PhaseListener {

    @Override
    public void afterPhase(PhaseEvent phaseEvent) {
        ExceptionHandler exceptionHandler = phaseEvent.getFacesContext().getExceptionHandler();

        // just debugging the handled exception, nothing here
        /*
        for (ExceptionQueuedEvent event : exceptionHandler.getHandledExceptionQueuedEvents()) {
            ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
            System.out.println("handled exception : " + context.getException());
        }*/

        for (Iterator<ExceptionQueuedEvent> it = exceptionHandler.getUnhandledExceptionQueuedEvents().iterator(); 
                it.hasNext();) {

            ExceptionQueuedEvent event = it.next();
            ExceptionQueuedEventContext eventContext = (ExceptionQueuedEventContext) event.getSource();
            Throwable e = eventContext.getException();
            System.out.println("unhandled exception : " + e);

            // get the root cause exception
            while (e.getCause() != null) { 
                e = e.getCause();
            }

            System.out.println("cause exception : " + e + 
                ", cause exception is BE : " + (e instanceof BusinessException));

            // handle BE
            if (e instanceof BusinessException) {
                BusinessException be = (BusinessException) e;
                System.out.println("processing BE " + be);
                FacesMessage message = Messages.getMessage(
                    "com.corejsf.errors", 
                    be.getMessage(), 
                    be.getParamValues()
                );
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(null, message);
                it.remove(); // remove the exception

                // works fine without this block, if BE is thrown, always return to the original page
                /*
                NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
                System.out.println("navigating to " + context.getViewRoot().getViewId());
                navigationHandler.handleNavigation(context, context.getViewRoot().getViewId(), null);
                */
            }
        }
    }

    @Override
    public void beforePhase(PhaseEvent phaseEvent) {
    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.INVOKE_APPLICATION;
    }

}

谢谢!

问候, 阿尔伯特·坎

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    不确定你为什么要走这条路,创建和注册你自己的异常处理程序真的很容易。尽管 Seam Catch 会更容易(http://seamframework.org/Seam3/CatchModulehttp://docs.jboss.org/seam/3/catch/3.0.0.Alpha1/reference/en-US/html_single/)。我还没有 JSF 桥接器,但是很容易做到。那么你所要做的就是编写一个处理特定异常的方法,你就完成了!

    【讨论】:

    • 我仍然是 jsf 的新手,对最佳实践充满疑问,因此上面的问题 :) 感谢您的建议,虽然我没有使用 seam 框架,但我要看看异常处理程序api.
    【解决方案2】:

    您的方法是 JSF 1.x 和 JSF 2.x 异常处理方法的混合。由于您使用的是 JSF 2.x,我建议您使用不带 PhaseListener 并借助新的 ExceptionHandler API 的纯 JSF 2.x 方法。

    您可以在 “JSF 2.0:完整参考”一书的第 282 页中找到处理 ViewExpiredException 的示例,该书可在线获得 here(单击第一个结果链接)。

    【讨论】:

    猜你喜欢
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多