【问题标题】:How to enable detailstheme to be applied on error page generated by exception handling?如何在异常处理产生的错误页面上启用detailstheme?
【发布时间】:2020-04-03 11:27:15
【问题描述】:

我使用过 Spring MVC 异常处理。 修改 web.xml 之类的

<error-page>
    <error-code>404</error-code>
    <location>/error</location>
</error-page>

@Controller
class GeneralErrorHandlerController {

    @RequestMapping("error")    
    public String handelError(final HttpServletRequest request, final Model model) {
         /**
      * Process the error details received in the request
     */

     // Lets get the status code and uri from the request
     final Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");      
     String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
          if (requestUri == null) {
        requestUri = "Unknown";
      }
      // create a message to be sent back via the model object.
      final String message = MessageFormat.format("{0} returned for {1}", 
        statusCode, requestUri); 

       model.addAttribute("errorMessage", message);
        return "Common/Error/globalError";
    }
}

但详细信息主题不适用于返回视图。所以请告诉我如何实现它。

【问题讨论】:

  • 您必须按照与其他页面完全相同的方式设计错误页面。如果您这样做,您的主题将被应用。也许您可以发布更多信息?
  • 我的问题是 Spring 和 SiteMesh 错误页面未装饰(跳过主过滤器)。你可以参考同样的问题,比如stackoverflow.com/questions/43593607/…
  • 显然你没有捕捉到 http 错误。在我的项目中,我实现了一个抽象基类,它捕获所有 http 错误并重定向到标准化的错误页面。下方谢赫回答
  • 无法获取。我已经按照 url baeldung.com/custom-error-page-spring-mvc 中定义的步骤进行操作 .. 我的问题是 detailstheme 没有应用于错误页面。

标签: java spring-mvc


【解决方案1】:

也许你可以尝试这样的事情。我为所有控制器实现了一个抽象基类。这个基类有一个例程来处理 servlet 遇到的所有 http 错误。

    @ExceptionHandler()
    public ModelAndView handleException(ServletException exception, HttpServletResponse response) {
    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

    // populate your modell with data, choose errorpage etc.

    }

【讨论】:

  • 怎么用?错误处理程序映射在哪里?
  • 这是错误处理。正如我所说,如果你把它放在你的控制器中(最好是在一个基本控制器中,它将这个方法继承给你拥有的所有其他控制器)所有 http 错误都会被捕获并显示你的错误页面。
猜你喜欢
  • 2017-04-05
  • 2015-02-04
  • 2014-07-25
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
相关资源
最近更新 更多