您的位置:首页技术文章
文章详情页

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

浏览:19日期:2023-07-30 18:16:10

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配置),如下

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

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

spring boot加载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模板路径的方法

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

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

标签: Spring
相关文章: