spring boot加載freemarker模板路徑的方法
1,之前用的eclipse開發(fā)工具來加載spring boot加載freemarker模板路徑,現(xiàn)在換用idea卻不能使用了,所以來記錄一下
加載freemarker模板三種方式,如下
public void setClassForTemplateLoading(Class clazz, String pathPrefix); public void setDirectoryForTemplateLoading(File dir) throws IOException; public void setServletContextForTemplateLoading(Object servletContext, String path);
看名字也就知道了,分別基于類路徑、文件系統(tǒng)以及Servlet Context。
第一種是我用idea,spring boot加載freemarker配置的
①首先設(shè)置spring boot加載freemarker模板的配置(代替了xml配置),如下

②通過Configuration來獲取freemarker文件路徑

這個(gè)方法是根據(jù)類加載路徑來判斷的,最終會(huì)執(zhí)行以下代碼
FreemarkerUtil.class.getClassLoader().getResource("/template/");
第二種基于文件系統(tǒng)。 比如加載/home/user/template下的模板文件。
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("/home/user/template"));
cfg.getTemplate("Base.ftl");
這樣就獲得了/home/user/template/Base.ftl這個(gè)模板文件
第三種基于web project。 第二個(gè)參數(shù)是基于WebRoot下的。
使用xml配置來看看


這里注意一下第二個(gè)參數(shù)需要以 “/” 開頭。
到此這篇關(guān)于spring boot加載freemarker模板路徑的文章就介紹到這了,更多相關(guān)spring boot freemarker模板路徑內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入了解Java線程池:從設(shè)計(jì)思想到源碼解讀
這篇文章將從設(shè)計(jì)思想到源碼解讀,帶大家深入了解Java的線程池,文中的示例代碼講解詳細(xì),對我們的學(xué)習(xí)或工作有一定的幫助,需要的可以參考一下2021-12-12
SpringBoot使用flyway初始化數(shù)據(jù)庫
這篇文章主要介紹了SpringBoot如何使用flyway初始化數(shù)據(jù)庫,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-03-03

