zoukankan      html  css  js  c++  java
  • 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 }
    杨河
  • 相关阅读:
    微服务
    flume
    mongodb
    Java 代理学习笔记
    HttpClient连接池
    一起写RPC
    spring boot cloud
    .NET MVC 表主外键关系 JSON 无限循环 方案二(推荐)
    EF提示“序列化类型为XXX的对象时检测到循环引用”
    Handlebars.js registerHelper
  • 原文地址:https://www.cnblogs.com/yang-he/p/13153614.html
Copyright © 2011-2022 走看看