处理 JSF 中未捕获的异常
我正在开发异常处理框架。异常处理框架适用于 JSF 应用程序。
我面临的问题是跟踪未捕获的异常并显示通用消息。我能够处理所执行操作的未捕获异常(例如单击按钮),但在加载 JSF 页面或初始化它时,我无法在框架级别捕获未捕获的运行时异常。任何帮助将不胜感激。
谢谢, 普拉萨德
I am developing exception handling framework. The exception handling framework is for a JSF application.
The problem I am facing is tracking uncaught exception and displaying a generic message. I am able to handle uncaught exception for the action that are carried out(like on a click of a button), but I am not able to catch uncaught runtime exception at framework level while loading JSF pages or while initializing it. Any help will really be appreciated.
Thanks,
Prasad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于你如何/在哪里捕捉它们。通常,您需要在
web.xml
中指定一个
,如下所示:这基本上显示了
error.html 页面。
另一种方法是在监听
/*
的url-pattern
的Filter
中捕获它:这基本上是相同的,你只需在控制响应和执行其他操作(例如日志记录)方面有更多的自由。
无论哪种方式,只要响应已经提交(即标头已经发送到客户端),它就会失败。然后,您应该注意到服务器日志中存在
IllegalStateException:响应已提交
,并且客户端将面临半生不熟(甚至空白)的页面。这是一个不归路。您希望在渲染响应之前执行任何业务逻辑。这也是在视图(JSP/Facelets)端执行业务逻辑被认为是不好的做法的原因之一。That depends on how/where you're catching them. Normally, you'd like to specify an
<error-page>
inweb.xml
for that like so:This basically shows the
error.html
page for anye instanceof java.lang.Exception
.Another way is to catch it in a
Filter
listening on anurl-pattern
of/*
:This does basically the same, you've only a bit more freedom in controlling the response and doing other stuff like logging.
Either way, it will fail whenever the response is already committed (i.e. the headers are already sent to the client side). You should then have noticed an
IllegalStateException: response already committed
in the server logs and the client will face a halfbaked (or even blank) page. This is a point of no return. You'd like to execute any business logic before rendering of the response. It's also one of the reasons that it's considered bad practice to do the business logic in the view (JSP/Facelets) side.