spring 如何从 src/main/resources 文件夹读取 Freemarker 模板文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31117107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read Freemarker Template files from src/main/resources folder?
提问by yathirigan
How to access my freemarker template (*.ftl) files stored within my src/main/resources folder from my code (Spring Boot application) ?
如何从我的代码(Spring Boot 应用程序)访问存储在我的 src/main/resources 文件夹中的我的 freemarker 模板 (*.ftl) 文件?
I tried the following
我尝试了以下
freemarker.template.Configuration config = new Configuration();
configuration.setClassForTemplateLoading(this.getClass(), "/resources/templates/");
and getting the following exception
并得到以下异常
freemarker.template.TemplateNotFoundException: Template not found for name "my-template.ftl".
回答by Nathan Hughes
The root of the classpath is src/main/resources
, change the path to
类路径的根是src/main/resources
,将路径更改为
configuration.setClassForTemplateLoading(this.getClass(), "/templates/");
回答by Ved Singh
I was facing "freemarker.template.TemplateNotFoundException: Template not found for name..." issue. My code was correct but I forgot to include /templates/ directory in pom.xml. So below code fixed the issue for me. I hope this helps.
我正面临着“freemarker.template.TemplateNotFoundException: Template not found for name...”问题。我的代码是正确的,但我忘记在 pom.xml 中包含 /templates/ 目录。所以下面的代码为我解决了这个问题。我希望这有帮助。
AppConfig.java :
@Bean(name="freemarkerConfiguration")
public freemarker.template.Configuration getFreeMarkerConfiguration() {
freemarker.template.Configuration config = new freemarker.template.Configuration(freemarker.template.Configuration.getVersion());
config.setClassForTemplateLoading(this.getClass(), "/templates/");
return config;
}
EmailSenderServiceImpl.java:
@Service("emailService")
public class EmailSenderServiceImpl implements EmailSenderService
{
@Autowired
private Configuration freemarkerConfiguration;
public String geFreeMarkerTemplateContent(Map<String, Object> dataModel, String templateName)
{
StringBuffer content = new StringBuffer();
try {
content.append(FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate(templateName), dataModel));
return content.toString();
}
catch(Exception exception) {
logger.error("Exception occured while processing freeMarker template: {} ", exception.getMessage(), exception);
}
return "";
}
}
pom.xml :
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources/</directory>
<includes>
<include>templates/*.ftl</include>
</includes>
</resource>
<resource>
<directory>src/main/</directory>
<includes>
<include>templates/*.ftl</include>
</includes>
</resource>
</resources>
</build>