欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

小白踩坑记:springboot运行一直报错:There was an unexpected error (type=Not Found, status=404).

程序员文章站 2022-03-03 17:05:54
...

唉,最近在学习springboot集成thymeleaf模板引擎时,明明都配置的好好的,却一直给爷报错,给小爷整懵了:更奇特的是,就挺秃然的就发现似乎哪里有问题了,一经改正,终于拨云见日了。。。
首先,我的controller和html页面反复好几遍,确认无误:
controller:


@Controller
@RequestMapping("/first")
public class firstController {
    @RequestMapping("/thyme")
    public  String thymeleaf(Model model){
        model.addAttribute("name","摘下梦中闪闪满天星");
        return "hello";
    }
}

hello.html:

// 引入xmlns:th="http://www.thymeleaf.org就可以使用thymeleaf的提示了
<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>欢迎</title>
</head>
<body>

    <div th:text="'为了ta要努力呀:'+${name}"></div>
</body>
</html>

springboot在引入thymeleaf依赖后,默认的thymeleaf的配置类中对模板进行了前后缀拼串,就不用像jsp一样去设置prefix和suffix了;默认的是resources目录下的templates文件夹下的html页面:

// ThymeleafProperties
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";

但是,天妒英才啊,运行总给我来个There was an unexpected error (type=Not Found, status=404).
也看了许多的博客,感觉我的和他们的错误情况不符;
朦胧之际,仿佛发现这个pom依赖似乎有点问题,和上面几个长得不大一样:

// 依赖
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.11.RELEASE</version>
        </dependency>

我这个是直接从thymeleaf官网拷贝的,没有和springboot整合起来,后果就是出错出错出错。。。

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Dec 14 11:18:37 CST 2020
There was an unexpected error (type=Not Found, status=404).

紧接着我就导入了springboot与thymeleaf相关的依赖,覆盖之前官网内个:

// springboot和thymeleaf整合的依赖
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

然后呢,再运行就成功啦~~~~~

为了ta要努力呀:摘下梦中闪闪满天星

经验教训与感想:
踩坑填坑才能不断进步。
小白学习实属不易,求个大佬带带我嘻嘻^ _ ^

============================================================================================

为了梦想,为了鸣鸣,冲啊????????????????????????