【问题标题】:Spring Boot - Thymeleaf - redirect to Error pageSpring Boot - Thymeleaf - 重定向到错误页面
【发布时间】:2020-12-02 21:05:19
【问题描述】:

我有一个 SpringBoot 应用程序。与百里香

我的属性文件中有这个:

# max file size - default 1MB
spring.servlet.multipart.max-file-size=10MB
# max request size - default 10MB
spring.servlet.multipart.max-request-size=25MB

这个控制器:

@Controller
public class MyErrorController implements ErrorController {

    private final static String PATH = "/error";

    @RequestMapping(PATH)
    @ResponseBody
    public String getErrorPath() {
        return "error/genericError";
    }


}

这个错误页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

<body>
 <div class="container">

        <div class="jumbotron text-center">

            <h1>Uh-oh! Something Happened!</h1>
            <!--  As we are using Thymeleaf, you might consider using
                  ${#httpServletRequest.requestURL}. But that returns the path
                  to this error page.  Hence we explicitly add the url to the
                  Model in some of the example code. -->
            <p th:if="${url}">
                <b>Page:</b> <span th:text="${url}">Page URL</span>
            </p>

            <p th:if="${timestamp}" id='created'>
                <b>Occurred:</b> <span th:text="${timestamp}">Timestamp</span>
            </p>

            <p>Devopsbuddy has encountered an error. Please contact support
                by clicking <a th:href="@{/contact}">here.</a></p>

            <p>Support may ask you to right click to view page source.</p>

            <!--
              // Hidden Exception Details  - this is not a recommendation, but here is
              // how you hide an exception in the page using Thymeleaf
              -->
            <div th:utext="'&lt;!--'" th:remove="tag"></div>
            <div th:utext="'Failed URL: ' +  ${url}" th:remove="tag">${url}</div>
            <div th:utext="'Exception: ' + ${exception.message}" th:remove="tag">${exception.message}</div>
            <ul th:remove="tag">
                <li th:each="ste : ${exception.stackTrace}" th:remove="tag"><span
                        th:utext="${ste}" th:remove="tag">${ste}</span></li>
            </ul>
            <div th:utext="'--&gt;'" th:remove="tag"></div>

        </div>

    </div>

</body>
</html>

但是当我尝试上传 > 10MB 的文件时

我没有看到错误页面,但是这个:

【问题讨论】:

    标签: java spring spring-boot spring-mvc thymeleaf


    【解决方案1】:

    请从你的控制器方法中移除@ResponseBody注解,这个注解表示返回的对象应该被自动序列化成JSON并传递回提供给客户端的响应中。

    您的代码应如下所示:

    @RequestMapping(PATH)
    public String getErrorPath() {
      return "error/genericError";
    }
    

    如@JustAnotherDeveloper 所示,error/genericError 与位于相应文件夹(在您的情况下为您的自定义错误页面)中的有效文件(HTML、jsp 等)匹配。

    【讨论】:

      【解决方案2】:

      你的返回变量是一个String,你想返回一个视图,像这样(假设Error是模板名):

          @RequestMapping(PATH)
          @ResponseBody
          public ModelAndView getErrorPath() {
                  return new ModelAndView("Error");
          }
      

      【讨论】:

      • 返回一个字符串很好,只要返回的字符串与位于相应文件夹(src/resources/templates,如果我没记错的话)中的 HTML 文件的名称相匹配。
      【解决方案3】:

      返回的错误页面必须正确映射:

      通过 Spring Boot application.properties 文件的一种方法:

      spring.mvc.view.prefix=/WEB-INF/
      spring.mvc.view.suffix=.jsp (or) html
      

      样品控制器:

      @GetMapping(value = "login")
      public String getLoginPage() {
          return "dummyPage";
      }
      

      【讨论】: