SpringBoot 使用thymeleaf 跳轉頁面時,總是提示404找不到頁面


Thymeleaf的使用和版本關系很大,如果路徑配置正確卻一直無法正常返回,可能就是版本錯誤導致。

想構建簡單頁面跳轉,正常跳轉index.html,404和500跳轉到專門的頁面,配置正確,路徑也正確,controller的方法也能訪問到,但是一直無法正常跳轉,頁面報404.修改版本后,可以正常訪問。

 

修改前:

@Controller
public class IndexController {

    @GetMapping("/")
    public String index() {
//        int i = 9 / 0;
        System.out.println("HelloWorld!");
        return "index";
    }
}

訪問:localhos:8080,報錯:404

 

記錄一下目前可以正常使用的版本(2020-12-23)

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>3.0.0</thymeleaf-layout-dialect.version>
</properties>

 修改后:

Controller(index):

@Controller
public class IndexController {

    @GetMapping("/")
    public String index() {
//        int i = 9 / 0;
        return "index";
    }
}

Controller(500):

@Controller
public class IndexController {

    @GetMapping("/")
    public String index() {
        int i = 9 / 0;
        return "index";
    }
}

測試404只需在導航欄輸入不存在的路徑,例如:http://localhost:8080/aaaa

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM