簡體   English   中英

Spring 引導 - Thymeleaf - 重定向到錯誤頁面

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

我有一個 SpringBoot 應用程序。 Thymeleaf

我的屬性文件中有這個:

# 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:

@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 的文件時

我沒有看到錯誤頁面,但是:

在此處輸入圖像描述

請從您的 controller 方法中刪除@ResponseBody注釋,此注釋表示返回的 object 應自動序列化為 JSON 並傳遞回提供給客戶端的響應。

您的代碼應如下所示:

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

如@JustAnotherDeveloper 所示, error/genericError匹配位於適當文件夾中的有效文件(HTML、jsp 等),在您的情況下,是您的自定義錯誤頁面。

你的返回變量是一個字符串,你想返回一個視圖,就像這樣(假設錯誤是模板名稱):

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

必須正確映射返回的錯誤頁面:

一種方法是通過 Spring 啟動 application.properties 文件:

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

樣本 Controller:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM