spring boot加载freemarker模板路径的方法

1,之前用的eclipse开发工具来加载spring boot加载freemarker模板路径,现在换用idea却不能使用了,所以来记录一下
加载freemarker模板三种方式,如下

public void setClassForTemplateLoading(Class clazz, String pathPrefix);

public void setDirectoryForTemplateLoading(File dir) throws IOException;

public void setServletContextForTemplateLoading(Object servletContext, String path);

看名字也就知道了,分别基于类路径、文件系统以及Servlet Context。
第一种是我用idea,spring boot加载freemarker配置的
①首先设置spring boot加载freemarker模板的配置(代替了xml配置),如下

②通过Configuration来获取freemarker文件路径

这个方法是根据类加载路径来判断的,最终会执行以下代码

FreemarkerUtil.class.getClassLoader().getResource("/template/");

第二种基于文件系统。 比如加载/home/user/template下的模板文件。

Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File("/home/user/template"));
cfg.getTemplate("Base.ftl");

这样就获得了/home/user/template/Base.ftl这个模板文件
第三种基于web project。 第二个参数是基于WebRoot下的。
使用xml配置来看看


这里注意一下第二个参数需要以 “/” 开头。

到此这篇关于spring boot加载freemarker模板路径的文章就介绍到这了,更多相关spring boot freemarker模板路径内容请搜索呐喊教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持呐喊教程!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。