spring boot加載freemarker模板路徑的方法
1,之前用的eclipse開(kāi)發(fā)工具來(lái)加載spring boot加載freemarker模板路徑,現(xiàn)在換用idea卻不能使用了,所以來(lái)記錄一下加載freemarker模板三種方式,如下
public void setClassForTemplateLoading(Class clazz, String pathPrefix);public void setDirectoryForTemplateLoading(File dir) throws IOException;public void setServletContextForTemplateLoading(Object servletContext, String path);
看名字也就知道了,分別基于類(lèi)路徑、文件系統(tǒng)以及Servlet Context。第一種是我用idea,spring boot加載freemarker配置的①首先設(shè)置spring boot加載freemarker模板的配置(代替了xml配置),如下
②通過(guò)Configuration來(lái)獲取freemarker文件路徑
這個(gè)方法是根據(jù)類(lèi)加載路徑來(lái)判斷的,最終會(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配置來(lái)看看
這里注意一下第二個(gè)參數(shù)需要以 “/” 開(kāi)頭。
到此這篇關(guān)于spring boot加載freemarker模板路徑的文章就介紹到這了,更多相關(guān)spring boot freemarker模板路徑內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. XML入門(mén)精解之結(jié)構(gòu)與語(yǔ)法2. HTML DOM setInterval和clearInterval方法案例詳解3. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera4. 輕松學(xué)習(xí)XML教程5. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理6. HTML <!DOCTYPE> 標(biāo)簽7. CSS 使用Sprites技術(shù)實(shí)現(xiàn)圓角效果8. XML入門(mén)的常見(jiàn)問(wèn)題(二)9. WMLScript的語(yǔ)法基礎(chǔ)10. 詳解CSS偽元素的妙用單標(biāo)簽之美
