Java Spring启动-Thymeleaf-重定向到错误页面

Java Spring启动-Thymeleaf-重定向到错误页面,java,spring,spring-boot,spring-mvc,thymeleaf,Java,Spring,Spring Boot,Spring Mvc,Thymeleaf,我有一个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 imple

我有一个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>

哦!出事了!

页面:页面URL

发生时间:时间戳

DevpsBuddy遇到错误。请联系支持部门 点击

支持人员可能会要求您右键单击以查看页面源

${url} ${exception.message}
  • ${ste}
但是当我试图上传一个大于10MB的文件时

我看不到错误页面,但是:


您的返回变量是一个字符串,您希望返回一个视图,如下所示(假设错误为模板名称):


您的返回变量是一个字符串,您希望返回一个视图,如下所示(假设错误为模板名称):


请从控制器方法中删除
@ResponseBody
注释,此注释表示返回的对象应自动序列化为JSON并传递回提供给客户端的响应

您的代码应该如下所示:

@RequestMapping(路径)
公共字符串getErrorPath(){
返回“错误/一般错误”;
}

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

请从控制器方法中删除
@ResponseBody
注释,此注释表示返回的对象应自动序列化为JSON,并传递回提供给客户端的响应中

您的代码应该如下所示:

@RequestMapping(路径)
公共字符串getErrorPath(){
返回“错误/一般错误”;
}

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

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

通过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";
}

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

通过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";
}

如果返回的字符串与相应文件夹(src/resources/templates,如果我的内存良好)中HTML文件的名称匹配,则返回字符串是可以的。如果返回的字符串与相应文件夹中HTML文件的名称匹配,则返回字符串是可以的(src/resources/templates,如果我的记性好的话)。