반응형
src/main/resources 폴더에서 프리마커 템플릿 파일을 읽는 방법
코드(Spring Boot 응용 프로그램)에서 src/main/resources 폴더에 저장된 프리마커 템플릿(*.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 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>
언급URL : https://stackoverflow.com/questions/31117107/how-to-read-freemarker-template-files-from-src-main-resources-folder
반응형
'programing' 카테고리의 다른 글
Json 태그는 있지만 내보내지 않음 (0) | 2023.04.04 |
---|---|
역할 편집자가 workdress에서 woocommerce 분류를 관리할 수 있도록 하려면 어떻게 해야 합니까? (0) | 2023.04.04 |
Wordpress admin menu messed (0) | 2023.04.04 |
TypeScript에서는 다양한 열거형이 어떻게 동작합니까? (0) | 2023.04.04 |
JSON에 대한 JavaScript 관련 배열 (0) | 2023.04.04 |