关于springboot:springboot访问静态页面

1次阅读

共计 700 个字符,预计需要花费 2 分钟才能阅读完成。

一、引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

二、动态资源地位以及优先级

1. 地位

  • static 目录:css、js、图片等
  • templates 目录:html 页面(templates 目录下的 html 页面不能间接拜访,须要通过服务器外部进行拜访,能够防止无权限的用户间接拜访到隐衷页面,造成信息泄露)

2. 优先级

spring boot 默认将 /** 动态资源拜访映射到以下目录:

classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources

这四个目录的拜访优先级:META-INF/resources > resources > static > public
拜访门路与 springboot.mvc.static-path-pattern、templates 下文件夹等无关

三、编写 Controller
1. 应用 @Controller,不能应用 @RestController

因为 @RestController 返回的是 @ResponseBody 的 json 数据,且不走 SpringMVC 的视图解析流程,所以跳不到 html 那里

四、前端打包资源重定向
应用 vue 等框架打包的单页面资源,能够应用相似 {module}-web 的拜访门路,并且在 controller 中应用通配门路 {modele}-web/** 指向 index.html。

正文完
 0