springboot MVC

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

maven依赖

项目结构

代码结构

配置文件(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)中添加了

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

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

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

使用thymeleaf则比较方便

添加的依赖

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

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

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

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

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

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

猜你喜欢

转载自blog.csdn.net/ssuperlg/article/details/81183766