programing

src/main/resources 폴더에서 프리마커 템플릿 파일을 읽는 방법

subpage 2023. 4. 4. 21:18
반응형

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

반응형