如何设置Spring Boot项目中Freemarker模板的加载路径?

2026-04-30 07:482阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计325个文字,预计阅读时间需要2分钟。

如何设置Spring Boot项目中Freemarker模板的加载路径?

1. 使用Eclipse时,曾通过以下路径加载Spring Boot和Freemarker模板: - `src/main/resources/templates`

2. 现切换到IDEA后,无法使用相同方式,以下为三种加载Freemarker模板的方法: - `public void setClassForTemplateLoading(Class clazz, String prefix)`

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模板路径内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

如何设置Spring Boot项目中Freemarker模板的加载路径?

本文共计325个文字,预计阅读时间需要2分钟。

如何设置Spring Boot项目中Freemarker模板的加载路径?

1. 使用Eclipse时,曾通过以下路径加载Spring Boot和Freemarker模板: - `src/main/resources/templates`

2. 现切换到IDEA后,无法使用相同方式,以下为三种加载Freemarker模板的方法: - `public void setClassForTemplateLoading(Class clazz, String prefix)`

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模板路径内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

如何设置Spring Boot项目中Freemarker模板的加载路径?