原文链接:https://blog.csdn.net/zmemorys/article/details/87275032
一.不用模板引擎的时候。
这里列出以下几种情况进行分析。
1. 情况1
【yml配置】
【controller】
【静态文件目录】
【结果】
【结论】:此时访问的error12.html是static目录下的 。 spring配置视图为的时候 controller只需要 return "error12";
2. 情况2
【yml配置】
【controller】
【静态文件目录】:不变
【结果】
【结论】此时访问的error12.html是static目录下的 。 spring配置视图为的时候 controller只需要 return "/error12";
3. 情况3
【yml配置】
【controller】
【静态文件目录】:不变
【结果】
【结论】此时访问的error12.html是static目录下的 。 spring配置视图为的时候 controller只需要 return "/error12.html";
springboot 默认资源根目录为static下 路径一定要写对不然出来一堆404还不知道为啥
关于静态资源的其他配置https://blog.csdn.net/yiifaa/article/details/78299052
1. “spring.mvc.static-path-pattern”
spring.mvc.static-path-pattern代表的含义是我们应该以什么样的路径来访问静态资源,换句话说,只有静态资源满足什么样的匹配条件,Spring Boot才会处理静态资源请求,以官方配置为例:
# 这表示只有静态资源的访问路径为/resources/**时,才会处理请求
spring.mvc.static-path-pattern=/resources/**,
假定采用默认的配置端口,那么只有请求地址类似于“http://localhost:8080/resources/jquery.js”时,Spring Boot才会处理此请求,处理方式是将根据模式匹配后的文件名查找本地文件,那么应该在什么地方查找本地文件呢?这就是“spring.resources.static-locations”的作用了。
2. “spring.resources.static-locations”
“spring.resources.static-locations”用于告诉Spring Boot应该在何处查找静态资源文件,这是一个列表性的配置,查找文件时会依赖于配置的先后顺序依次进行,默认的官方配置如下:
spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources
继续以上面的请求地址为例,“http://localhost:8080/resources/jquery.js”就会在上述的四个路径中依次查找是否存在“jquery.js”文件,如果找到了,则返回此文件,否则返回404错误。
二.使用thymeleaf模板引擎的时候
步骤1.引入jar包 (当引入这个jar包的时候 默认的静态根目录变成了templates)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
步骤二:application.yml只需要加(开发时禁用缓存)
spring:
thymeleaf:
cache: false
步骤三:写controller
步骤四:templates下放html error12.html
结论:
附上html代码
备注:
1. controller上的注解注意,不能是 @RestController,否则,接口返回的就直接是“/error12.html”这样的 String了
2. 参考过其他文章:https://blog.csdn.net/moshowgame/article/details/82869151
参考这个没有走通,把文件夹调成示例中的 tamplates也不行,可能是少了什么配置?参考 https://blog.csdn.net/fuck487/article/details/79298556 这个,感觉也没啥特别需要注意的配置,应该能走通的
其他参考:
https://blog.csdn.net/wuxue_123/article/details/52662145