JSF Ajax请求中的异常处理

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

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


慕村225694
浏览 555回答 2
2回答

天涯尽头无女友

OmniFaces FullAjaxExceptionHandler展示柜中充实了这个问题。基本上,您需要创建一个定制,ExceptionHandler因为标准JSF不会提供一个定制的定制(至少不是明智的选择)。在其中,您将可以接触Exception引起所有麻烦的实例。这是一个启动示例:public class YourExceptionHandler extends ExceptionHandlerWrapper {&nbsp; &nbsp; private ExceptionHandler wrapped;&nbsp; &nbsp; public YourExceptionHandler(ExceptionHandler wrapped) {&nbsp; &nbsp; &nbsp; &nbsp; this.wrapped = wrapped;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void handle() throws FacesException {&nbsp; &nbsp; &nbsp; &nbsp; FacesContext facesContext = FacesContext.getCurrentInstance();&nbsp; &nbsp; &nbsp; &nbsp; for (Iterator<ExceptionQueuedEvent> iter = getUnhandledExceptionQueuedEvents().iterator(); iter.hasNext();) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Throwable exception = iter.next().getContext().getException(); // There it is!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Now do your thing with it. This example implementation merely prints the stack trace.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exception.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You could redirect to an error page (bad practice).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Or you could render a full error page (as OmniFaces does).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Or you could show a FATAL faces message.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Or you could trigger an oncomplete script.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // etc..&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; getWrapped().handle();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public ExceptionHandler getWrapped() {&nbsp; &nbsp; &nbsp; &nbsp; return wrapped;&nbsp; &nbsp; }}为了使其运行,请ExceptionHandlerFactory如下创建一个自定义:public class YourExceptionHandlerFactory extends ExceptionHandlerFactory {&nbsp; &nbsp; private ExceptionHandlerFactory parent;&nbsp; &nbsp; public YourExceptionHandlerFactory(ExceptionHandlerFactory parent) {&nbsp; &nbsp; &nbsp; &nbsp; this.parent = parent;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public ExceptionHandler getExceptionHandler() {&nbsp; &nbsp; &nbsp; &nbsp; return new YourExceptionHandler(parent.getExceptionHandler());&nbsp; &nbsp; }}需要进行以下注册faces-config.xml:<factory>&nbsp; &nbsp; <exception-handler-factory>com.example.YourExceptionHandlerFactory</exception-handler-factory></factory>
打开App,查看更多内容
随时随地看视频慕课网APP