您的当前位置:首页springboottemplates目录下的页面文件访问不到static目录下的jq文件

springboottemplates目录下的页面文件访问不到static目录下的jq文件

来源:爱问旅游网
springboottemplates⽬录下的页⾯⽂件访问不到static⽬录下的jq⽂

  springboot项⽬结构中,页⾯(eg:.ftl )放在 templates ⽬录下,静态⽂件 (eg : .js) 放在 static ⽬录下, 但是实际操作中发现 controller 访问到 ftl页⾯后, 找不到引⽤ static ⽬录中的 jq ⽂件  然后开始了尴尬的⼀个早上。。。  

  如果你遇到了这个问题,你⾸先要想到要把 springboot 中访问静态资源的路径映射到 static ⽬录下, 对资源访问做⼀个映射,然后去百度吧。。。

  在 application.properties ⽂件中加⼊以下资源映射:

  

  贴上项⽬结构,让⼀起⼊门的⼩伙伴们看下结构,⼼理会踏实点 (我的⽬的就是要在 index.ftl ⽂件中访问到 static/js ⽬录下的jq⽂件):

  

  其实好像添加上这个配置后就⾏了的。。。

  你可以通过继承 WebMvcConfigurerAdapter (这个类已经显⽰ deprecated, 但是没找到替代的新类 ), 实现 addResourceHandlers ⽅法添加对静态资源访问的路径

  

  如果你只是要引⽤jquery⽂件的话,可以通过 webjars 的形式, 在pom⽂件中 添加依赖 1

2 org.webjars

3 webjars-locator-core 4 5

6 org.webjars 7 bootstrap 8 3.3.7 9 10

11 org.webjars12 jquery13 3.1.114

  然后直接在 ftl 页⾯中加⼊对所需⽂件的引⽤:  

  *************** 需要注意的点 : 添加webjars, 需要在 addResourcesHandlers ⽅法中, 把 /resources ⽬录加上去 (这个导⼊jq⽂件后,看下jq⽂件的实际⽬录就知道了)

  最后加上springboot 整合 ftl 的配置:spring.mvc.static-path-pattern=/static/**

spring.resources.static-locations=classpath:/static/spring.freemarker.allow-request-override=false#Enable template caching.启⽤模板缓存。spring.freemarker.cache=false

spring.freemarker.check-template-location=truespring.freemarker.charset=UTF-8spring.freemarker.content-type=text/html

spring.freemarker.expose-request-attributes=falsespring.freemarker.expose-session-attributes=false

spring.freemarker.expose-spring-macro-helpers=false#spring.freemarker.prefix=

#spring.freemarker.request-context-attribute=#spring.freemarker.settings.*=#设置⾯板后缀

spring.freemarker.suffix=.ftl

  最后⼀个 freemarker.suffix 设置看访问的页⾯后缀了,这⾥是 ftl 就设置成 ftl, 否则访问不到   整合 freemarker , pom⽂件中加上

org.springframework.boot

spring-boot-starter-freemarker  

  今天遇到了⼀个⼩坑,是我太菜,弄了⼀个早上。。。

因篇幅问题不能全部显示,请点此查看更多更全内容