如何吞掉 JSF 1.2/Mojarra 中的异常

发布于 2024-12-15 08:10:42 字数 943 浏览 3 评论 0原文

ViewExpiredException 正在通过重定向到登录屏幕来处理。问题是异常仍然被记录,并且客户强烈希望 server.log 没有异常。

虽然在这种情况下这可能是一个有问题的要求,但我仍然必须实现它。我们使用 Mojarra 并在 JBoss EAP 5.1 上部署

MyFaces 方法< /a> 没有帮助,因为我显然无法使用 Mojarra 包装 MyFacesServlet

我无法应用 JBoss JSF 指南 包装 Faces servlet,因为我在任何地方都找不到 jsf-integration-deployer-jboss-beans.xml。

我无法获得 Ed Burns 提出的 方法 工作。我猜原因是它是针对 JSF2 的,因为我在 jar 中找不到 javax.faces.context.ExceptionHandlerFactory 。

更糟糕的是,我对 JSF 还很陌生,所以我必须依赖详细的指导,在寻找这些指导时我找到了上述方法,但未能应用它们。

谢谢

The ViewExpiredException is being handled by a redirect to the login screen. The problem is that the exception is still logged, and the customer would strongly like to have the server.log exception-free.

While it may be a questionable requirement in this case, I still have to make it happen. We use Mojarra and deploy on JBoss EAP 5.1

The MyFaces approach does not help as I obviously cannot wrap the MyFacesServlet using Mojarra

I could not apply the advice given in the JBoss JSF guide to wrap the Faces servlet as I cannot find the jsf-integration-deployer-jboss-beans.xml anywhere.

I cannot get the approach proposed by Ed Burns to work either. I guess the reason is that it is targeted at JSF2 for I cannot find the javax.faces.context.ExceptionHandlerFactory in my jars.

Making the matter worse, I am quite new to JSF, so I have to rely on detailed guidance, in search of which I have found the above approaches but failed to apply them.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就此别过 2024-12-22 08:10:42

那篇文章确实是针对 JSF 2.x 的。 JSF 1.x 没有任何异常处理工具。

只需捕获并重定向您自己,而不是让容器来做。过滤器是一个明智的选择:

try {
    chain.doFilter(request, response);
} catch (ServletException e) {
    if (e.getRootCause() instanceof ViewExpiredException) {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        res.sendRedirect(req.getContextPath() + "/errors/session-expired.jsf");
    } else {
        throw e;
    }
}

web.xml 中将其映射到 FacesServlet 的 servlet 名称上以使其运行。

That article is indeed targeted on JSF 2.x. JSF 1.x does not have any exception handling facility.

Just catch and redirect yourself instead of letting the container do. A filter is a sensible place for this:

try {
    chain.doFilter(request, response);
} catch (ServletException e) {
    if (e.getRootCause() instanceof ViewExpiredException) {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        res.sendRedirect(req.getContextPath() + "/errors/session-expired.jsf");
    } else {
        throw e;
    }
}

Map this in web.xml on the servlet name of the FacesServlet to get it to run.

靖瑶 2024-12-22 08:10:42

我在 JSF 2.0 中遇到了同样的问题,实现是由 IBM Websphere 8.5.5.3 提供的。

我尝试使用 ExceptionHandlerWrapper 来检查 ViewExpiredException 和
重定向到会话过期页面(或登录页面),但我无法消除以下错误,遗憾的是此错误在处理程序之前打印:

StateUtils E View State cannot be reconstructed

但我设法通过实现一个 servlet 过滤器来解决它,该过滤器将检查任何潜在的 ViewExpiredException,它将其重定向到会话过期页面(如果有)。

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

HttpSession session = request.getSession(false);

boolean requestContainsViewState = (request.getParameter("javax.faces.ViewState") != null);
boolean potentialViewExpiredException = (session == null && requestContainsViewState);
if (potentialViewExpiredException) {
    // No session found and potential ViewExpiredException, redirect to session expired page.
    response.sendRedirect(request.getContextPath() + "/pages/error/expired.xhtml");
} else {
    chain.doFilter(req, res);
}

编辑:您需要确保这是过滤器链中的第一个过滤器,如果存在先前的过滤器并且此过滤器创建了一个会话,则上述解决方案将不适用于您。

I faced the same issue in JSF 2.0, the implementation is provided by the IBM Websphere 8.5.5.3 .

I tried to use an ExceptionHandlerWrapper to check for the ViewExpiredException and
redirect to session expired page (or login page), but I couldn't silence the below error, sadly this error is printed before the handler:

StateUtils E View State cannot be reconstructed

But I manged to solve it by implementing a servlet filter which will check for any potential ViewExpiredException, it will redirect it to session expired page if any.

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

HttpSession session = request.getSession(false);

boolean requestContainsViewState = (request.getParameter("javax.faces.ViewState") != null);
boolean potentialViewExpiredException = (session == null && requestContainsViewState);
if (potentialViewExpiredException) {
    // No session found and potential ViewExpiredException, redirect to session expired page.
    response.sendRedirect(request.getContextPath() + "/pages/error/expired.xhtml");
} else {
    chain.doFilter(req, res);
}

Edit : you need to make sure that this is the first filter in the filters chain, if there's a previous filter and this filter created a session the above solution will not work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文