欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-07-09 08:27:07|栏目:JAVA代码|点击:

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配置来看看

在这里插入图片描述
在这里插入图片描述

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

上一篇:java的jdk基础知识点总结

栏    目:JAVA代码

下一篇:IDEA中Git的基本应用详解

本文标题:spring boot加载freemarker模板路径的方法

本文地址:http://www.codeinn.net/misctech/154779.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有