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

 

原文地址:https://www.cnblogs.com/Bernard94/p/14178050.html