昨天在springboot的demo中遇到了mvc的跳转问题

 

springboot MVC

maven依赖

 

springboot MVC

项目结构

 

springboot MVC

代码结构

 

springboot MVC

配置文件(application.properties)

 

这种的情况下,springboot会默认访问static下的文件

因此可以访问的到 head.html。

且默认的springboot会查找static下的名为index.html的页面当做入口

即localhost:8080/ 会跳转到index.html

templates下的两个页面文件无法被访问

倘若 index.html放入templates文件下,则localhost:8080/ 不会被跳转到index.html

即不配置路劲的情况下,访问的都是static下的文件

还有一种情况是画蛇添足的,即在配置文件(application.properties)中添加了

springboot MVC

这样添加了前缀,访问页面时,路劲为 static文件下的/static/head.html

这样是无法访问,报404错误。

但是即使你配置错了路径,springboot依然找的到 index.html。

 

使用thymeleaf则比较方便

springboot MVC

添加的依赖

 

使用该模板后,默认的路劲变成了templates文件

在不改动别的配置的情况下,body.html 和 test.html可以被访问

同时,在static文件下的head.html则无法被访问。

当然了 index.html 依然可以在入口(localhost:8080/)被访问

 

倘若想访问在static下的文件则在applcation.properties文件添加

springboot MVC

即可访问static下的文件(原templates下的文件即无法被访问)

相关文章:

  • 2021-05-29
  • 2022-12-23
  • 2021-04-24
  • 2021-10-27
  • 2021-11-03
  • 2021-10-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-21
  • 2021-05-19
相关资源
相似解决方案