Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何从src/main/resources文件夹读取Freemarker模板文件?_Spring_Maven_Spring Boot_Freemarker - Fatal编程技术网

Spring 如何从src/main/resources文件夹读取Freemarker模板文件?

Spring 如何从src/main/resources文件夹读取Freemarker模板文件?,spring,maven,spring-boot,freemarker,Spring,Maven,Spring Boot,Freemarker,如何从我的代码(Spring Boot应用程序)访问存储在我的src/main/resources文件夹中的freemarker模板(*.ftl)文件 我尝试了以下方法 freemarker.template.Configuration config = new Configuration(); configuration.setClassForTemplateLoading(this.getClass(), "/resources/templates/"); 并获得以下异常 freemark

如何从我的代码(Spring Boot应用程序)访问存储在我的src/main/resources文件夹中的freemarker模板(*.ftl)文件

我尝试了以下方法

freemarker.template.Configuration config = new Configuration();
configuration.setClassForTemplateLoading(this.getClass(), "/resources/templates/");
并获得以下异常

freemarker.template.TemplateNotFoundException: Template not found for name "my-template.ftl".

类路径的根是
src/main/resources
,将路径更改为

configuration.setClassForTemplateLoading(this.getClass(), "/templates/");
我面临“freemarker.template.TemplateNotFoundException:找不到名称的模板…”问题。 我的代码是正确的,但我忘记在pom.xml中包含/templates/directory。下面的代码为我解决了这个问题。我希望这有帮助

AppConfig.java:
@Bean(name=“freemarkerConfiguration”)
public freemarker.template.Configuration GetFreeMarker配置(){
freemarker.template.Configuration config=new freemarker.template.Configuration(freemarker.template.Configuration.getVersion());
config.setClassForTemplateLoading(this.getClass(),“/templates/”;
返回配置;
}
EmailSenderServiceImpl.java:
@服务(“电子邮件服务”)
公共类EmailSenderService Impl实现EmailSenderService
{
@自动连线
私有配置Freemarker配置;
公共字符串geFreeMarkerTemplateContent(映射数据模型,字符串templateName)
{
StringBuffer内容=新的StringBuffer();
试一试{
append(FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate(templateName),dataModel));
返回content.toString();
}
捕获(异常){
logger.error(“处理freeMarker模板时发生异常:{}”,Exception.getMessage(),Exception);
}
返回“”;
}
}
pom.xml:
org.springframework.boot
弹簧启动机自由标记器
org.freemarker
自由标记
编译
src/main/resources/
模板/*.ftl
总工程师/总工程师/
模板/*.ftl

但是如果我们从jar文件运行代码,如何修改代码以适应它?比如:java-jar freemarker.jar,在这个jar文件中,模板文件夹位于这个jar包的根目录中new ClassTemplateLoader(getClass(),“/templates”)@AlterHu,理想情况下,您应该将src/main/resources放在jar文件中。@我完全同意您的意见:如果使用开箱即用的Spring配置,您实际上不需要做任何事情。因为Spring boot为您配置了包括解析器在内的空闲标记。