Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a Spring webmvc project, use a extern folder for the templates

I have tried to externalize the template folder of my spring webmvc proyect, I need this feature because the designer want to modify very often the html. And is to dificult for him to edit inside de a war file. I use thymeleaf as templateResolver

<bean id="templateResolver" 
    class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">

    <property name="prefix" value="file:/opt/templates/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML5" />
    <property name="cacheable" value="false" />
</bean>

I tried using this line, but it didn't work

<property name="prefix" value="file:/opt/templates/" />

How can I do that?

like image 992
Troncador Avatar asked Jan 13 '23 11:01

Troncador


2 Answers

You can use FileTemplateResolver instead of ServletContextTemplateResolver.

Try this:

<bean id="templateResolver" 
    class="org.thymeleaf.templateresolver.FileTemplateResolver">

    <property name="prefix" value="/opt/templates/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML5" />
    <property name="cacheable" value="false" />
</bean>
like image 88
Shinichi Kai Avatar answered Jan 22 '23 22:01

Shinichi Kai


The class version :

@Configuration
public class TemplateEngineConfig {
    @Value("${templates.path}")
    private String HTML_TEMPLATES_PATH;

    @Bean
    public TemplateEngine templateEngine() {
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        return templateEngine;
    }

    private TemplateResolver templateResolver() {
        TemplateResolver resolver = new FileTemplateResolver();
        resolver.setPrefix("/opt/templates/");
        resolver.setSuffix(".html");
        resolver.setCacheable(false);
        return resolver;
    }
}
like image 41
abahet Avatar answered Jan 22 '23 20:01

abahet