【问题标题】:Exception and error handling configuration through web.xml通过 web.xml 配置异常和错误处理
【发布时间】:2013-05-24 12:55:12
【问题描述】:

我使用的是 Apache MyFaces,需要以不同于其他内部服务器错误的方式处理 ViewExpiredException。但我发现如果我包含错误代码,则为 500;然后在 ViewExpiredException 错误中也采用错误代码的路径。

下面是web.xml配置:

<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/login.xhtml?faces-redirect=true</location>
</error-page> 

<error-page>
    <error-code>500</error-code>
    <location>/error.xhtml</location>
</error-page>

对于上述两种情况,如何确保我可以重定向到不同的 URL? 参考multiple error-code configuration web.xml,我可以用单个 servlet 替换该位置;但是如何捕获 servlet 中的错误呢?

【问题讨论】:

    标签: jsf jsf-2 web.xml


    【解决方案1】:

    对于 ViewExpiredException 为什么不尝试通过 CustomExceptionHandler 处理异常并通过 handle() 方法将用户重定向到您想要的任何页面

    【讨论】:

    • 你需要用代码示例更好地解释你的答案
    • @hananAhmed:请分享示例
    【解决方案2】:

    我在这里遇到了 2 个问题:

    一个。识别 javax.faces.application.ViewExpiredException 并重定向到 /login.xhtml?faces-redirect=true。如果没有 faces-redirect,重定向到 XHTML 页面会抛出错误

    b.确保将状态码为 500 的其他错误重定向到相应的页面。

    这些是通过以下方式解决的:

    1> 创建了一个 servlet 来检查 HTTP 代码,然后检查错误是否是由于 ViewExpiredException 引起的。根据错误情况,servlet 将请求转发到特定的 URL。

    if (request.getAttribute("javax.servlet.error.status_code") == 500) {
          excep = (Class<? extends Exception>) request
                  .getAttribute("javax.servlet.error.exception_type");
          if (excep != null) {
                if (excep.getCanonicalName().equalsIgnoreCase(
                    "javax.faces.application.ViewExpiredException")) {
                  //you can forward to another page like /login.xhtml?faces-redirect=true
                } else {
                 //you can forward to different page with error message
                }
          }
          }
    

    2> web.xml 中的更改以将所有 500 异常转发到此 servlet

    <error-page>
        <error-code>500</error-code>
        <location>/error</location><!-- here error is name of servlet -->
    </error-page>
    

    【讨论】:

      猜你喜欢
      • 2014-04-06
      • 2012-09-15
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      相关资源
      最近更新 更多