Handling Uncaught Exception in JSF

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-31 21:55:29

问题


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


回答1:


That depends on how/where you're catching them. Normally, you'd like to specify an <error-page> in web.xml for that like so:

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/error.html</location>
</error-page>

This basically shows the error.html page for any e instanceof java.lang.Exception.

Another way is to catch it in a Filter listening on an url-pattern of /*:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    try {
        chain.doFilter(request, response);
    } catch (Exception e) {
        request.setAttribute("exception", e);
        request.getRequestDispatcher("/error.html").forward(request, response);
    }
}

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.



来源:https://stackoverflow.com/questions/2999065/handling-uncaught-exception-in-jsf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!