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

唉,最近在学习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要努力呀:摘下梦中闪闪满天星

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

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

为了梦想,为了然宝,冲啊😃😃😃😃😃😃

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值