你看到的是今天的我,昨天的我已经死了

人生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。

SpringBoot不能直接访问templates下的静态资源

Spriongboot创建的项目,在resources  -> templates下的资源是不能直接访问的,没有开放访问权限。这是因为templates文件夹,是放置模板文件的,因此需要视图解析器来解析它。所以必须通过服务器内部进行访问,也就是要走控制器  ->  服务  ->  视图解析器这个流程才行。同时,存在安全问题。比如说,你把你后台的html文件放到templates,而这个文件夹对外又是开放的,就会存在安全隐患。

 

  •   这里提供两种可以方式访问templates模板下的资源文件
  •   方式一:在application.yml或者application.properties配置文件中将访问权限开放(这种方式不推荐)
spring:
  resources:
    static-locations: classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/, classpath:/templates/

  或者

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/
  • 方式二:通过内部controller跳转访问的资源

比如要访问templates下的test.html。先请求内部控制器,由控制器跳转test.html页面

1 @Controller
2 public class Test {
3 
4     @GetMapping("/test")
5     public String test() {
6         return "test";
7     }
8 }

 

posted on 2020-06-17 17:18  橘子味的猫啊  阅读(3342)  评论(0编辑  收藏  举报

导航