简体   繁体   中英

Spring Boot - Thymeleaf - redirect to Error page

I have a SpringBoot app. with 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
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

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.

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.

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.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp (or) html

Sample Controller:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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