3

I'm working with spring boot and thymeleaf to generate Documents from html templates.

As the templates continuously changes, i want ti to load templates from an external just to add or remove templates from there instead of redeploy the application.

As a POC, when using /resources folder works fine.

This is the error:

Error resolving template "voucher", the template might not exist or might not be accessible by any of the configured Template Resolvers

This is the context:

applycation.yml

spring:
  thymeleaf:
    prefix: file:///${PARAMETERS_DIRECTORY_TEMPLATES:/home/app/templates/}
    check-template-location: true
    suffix=: .html
    mode: HTML
    encoding: UTF-8

This is my Method: Where templateName is the template filename and parameters is just a map who has the values to be replaced by the engine.

 @Override
public String buildHtmlFromTemplate(String templateName, Map<String, String> parameters) {
    TemplateEngine templateEngine = new TemplateEngine();
    FileTemplateResolver templateResolver = new FileTemplateResolver ();
    templateResolver.setOrder(templateEngine.getTemplateResolvers().size());
    templateResolver.setCacheable(false);
    templateResolver.setCheckExistence(true);

    templateEngine.setTemplateResolver(templateResolver);

    return templateEngine.process(templateName, this.resolveHtmlTemplateAttributesContext(parameters));
}

NOTE: I removed tha applycation yml thymeleaf configs and implemented next code but the error persists.

@Override
public String buildHtmlFromTemplate(String templateName, Map<String, String> parameters) {
    TemplateEngine templateEngine = new TemplateEngine();
    FileTemplateResolver templateResolver = new FileTemplateResolver ();
    templateResolver.setPrefix("/home/skeeter/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML");
    templateResolver.setOrder(templateEngine.getTemplateResolvers().size());
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setCacheable(false);
    templateResolver.setCheckExistence(true);

    templateEngine.setTemplateResolver(templateResolver);

    return templateEngine.process(templateName, this.resolveHtmlTemplateAttributesContext(parameters));
}
4
  • 1
    This will resolve your request forum.thymeleaf.org/… Commented Aug 10, 2020 at 14:52
  • Thanks @SounakSaha i had read this article. I think that i'm using the right resolver but can't understan what i'm missing. Commented Aug 10, 2020 at 15:05
  • 2
    When setting the prefix in Java code for a FileTemplateResolver, you use a simple file path - for example, /path/to/templatesDir/ - without the URL-style use of file://. I don't know if the syntax is different just because you are using Spring's application.yml. Try commenting out the prefix line in the config file, and adding templateResolver.setPrefix("/path/to/templatesDir/"); in your Java code, as a test. Commented Aug 10, 2020 at 17:46
  • @andrewjames i tried moving all configs to the code but the error persists Commented Aug 10, 2020 at 22:24

2 Answers 2

3

In my case I was using the ClassLoaderTemplateResolver, but after I changed it to FileTemplateResolver I can use the absolute path and it works fine.

Double check that you have read permissions in the directory where you have the templates.

Sign up to request clarification or add additional context in comments.

Comments

3

Curiously, the issue was solved using this code and the /usr/app/templates created with sudo. I think it was only a permissions issue

.....
@Value("${parameters.directory.templates}")
private String templatesDirectory;  
.....    


@Override
public String buildHtmlFromTemplate(String templateName, Map<String, String> parameters) {
    TemplateEngine templateEngine = new TemplateEngine();
    FileTemplateResolver templateResolver = new FileTemplateResolver ();
    templateResolver.setPrefix(templatesDirectory);
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML");
    templateResolver.setOrder(templateEngine.getTemplateResolvers().size());
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setCacheable(false);
    templateResolver.setCheckExistence(true);

    templateEngine.setTemplateResolver(templateResolver);

    return templateEngine.process(templateName, this.resolveHtmlTemplateAttributesContext(parameters));
}

2 Comments

i am still facing this issue
Give us more info please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.