简体   繁体   English

Spring 引导 - Thymeleaf - 重定向到错误页面

[英]Spring Boot - Thymeleaf - redirect to Error page

I have a SpringBoot app.我有一个 SpringBoot 应用程序。 with Thymeleaf Thymeleaf

I have this in my property file:我的属性文件中有这个:

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

this controller:这个 controller:

@Controller
public class MyErrorController implements ErrorController {

    private final static String PATH = "/error";

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


}

and this Error page:和这个错误页面:

<!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>

but when I try to upload a file > 10MB但是当我尝试上传大于 10MB 的文件时

I don't see the error Page, but this:我没有看到错误页面,但是:

在此处输入图像描述

Please, remove the @ResponseBody annotation from your controller method, this annotation indicates that the object returned should be automatically serialized into JSON and passed back into the response offered to the client.请从您的 controller 方法中删除@ResponseBody注释,此注释表示返回的 object 应自动序列化为 JSON 并传递回提供给客户端的响应。

Your code should look like:您的代码应如下所示:

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

Where, as indicated by @JustAnotherDeveloper, error/genericError matches a valid file (HTML, jsp, etcetera) located in the appropriate folder, in your case, your custom error page.如@JustAnotherDeveloper 所示, error/genericError匹配位于适当文件夹中的有效文件(HTML、jsp 等),在您的情况下,是您的自定义错误页面。

Your return variable is a String, you want to return a view, like this (assuming Error is template name):你的返回变量是一个字符串,你想返回一个视图,就像这样(假设错误是模板名称):

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

Returned Error page must be mapped properly:必须正确映射返回的错误页面:

One way to do it through Spring Boot application.properties file:一种方法是通过 Spring 启动 application.properties 文件:

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

Sample Controller:样本 Controller:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM