关于Springboot配置thymeleaf的404问题

Terwer...小于 1 分钟经验分享实用技巧error404springbootthymeleaf

问题

我的yaml配置

server:
  port: 8080
resources:
  static-locations:
    - classpath:/static/
spring:
  thymeleaf:
    cache: false
    checktemplatelocation: true
    enabled: true
    encoding: UTF-8
    mode: HTML
    prefix: classpath:/templates/
    suffix: .html

controller目录结构

image-20220430234546959
image-20220430234546959

原因

其实是一个非常简单的错误,找了半天。

我么默认创建的Springboot项目Application启动入口类目录是在项目包下面的。如果我们创建的controller不是项目包的子包的话,那么自动扫描就不会扫码controller类,从而导致conroller无法注册,访问肯定就404了。

解决

调整controller包的位置,使它位于Application所在包的子包下面即可。

image-20220430235031452
image-20220430235031452

这样问题就解决了。

评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.14.9