看公司代碼時的一些記錄

freemarker中加載不了其他文件,服務器顯示404.
這裏寫圖片描述

1.ApplicationListener 監聽器

2.容器加載完畢做一件事(利用ContextRefreshedEvent事件):很多時候我們想要在某個類加載完畢時幹某件事情,但是使用了spring管理對象,我們這個類引用了其他類(可能是更復雜的關聯),所以當我們去使用這個類做事情時發現報空指針錯誤,這是因爲我們這個類有可能已經初始化完成,但是引用的其他類不一定初始化完成,所以發生了空指針錯誤

3.WebMvcConfigurerAdapter

4.freemarker.template.Configuration.setSharedVariable()

官方api是這樣說的
SharedVariables are variables that are defined for all templates..
You can add shared variables to the configuration with the setSharedVariable methods:

Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);
...
cfg.setSharedVariable("warp", new WarpDirective());
cfg.setSharedVariable("company", "Foo Inc.");

問題解決:application.yml中的classic_compatible和auto_import缺一不可

freemarker:
    content-type: text/html
    suffix: .html
    settings:
      classic_compatible: true
      auto_import: /ftl/pony/index.ftl as p,/ftl/spring.ftl as s,/pages/global/ztitle.html as z

下面這句話的意思是頁面文件中引入的外部文件都放在/mag/res文件夾中

        cfg.setSharedVariable("res", propMy.getResbase());
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章