Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error encountered while using thymeleaf as email templating #93

Closed
berinle opened this issue Oct 15, 2013 · 1 comment
Closed

Error encountered while using thymeleaf as email templating #93

berinle opened this issue Oct 15, 2013 · 1 comment

Comments

@berinle
Copy link

berinle commented Oct 15, 2013

Hi -
I am trying to use thymeleaf as the template engine for spring boot. I have followed this guide (http://www.thymeleaf.org/springmail.html) and the issue I am running into is that thymeleaf can't seem to resolve the variables set in org.thymeleaf.context.Context when I make the call to process the template.

I get errors like this...

org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "user" (email.html:7)
at org.thymeleaf.spring3.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:108)
at org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:116)
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:394)

....

I have the sample application here https://github.com/berinle/spring-boot-sample

It can be run by cloning the project and "cd" into it and run the following command: "./gradlew run" (without the quotes). Then open your browser and hit http://localhost:8080.

Click the "send email" button and you should see the error on the console.

Also a broader question is what is the best way to easily override the generated "templateEngine" bean created by @EnableAutoConfiguration?

On the "dev" branch of the same project above, I have configured the following beans...

@Bean
public ServletContextTemplateResolver webTemplateResolver() {
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
    templateResolver.setPrefix("templates/");
    templateResolver.setTemplateMode("HTML5");
    templateResolver.setSuffix(".html");
    templateResolver.setOrder(2);
    templateResolver.setCacheable(true);

    return templateResolver;
}

@Bean
public SpringTemplateEngine templateEngine(){
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(emailTemplateResolver());
    templateEngine.addTemplateResolver(webTemplateResolver());
    return templateEngine;
}

@Bean
public ThymeleafViewResolver thymeleafViewResolver(){
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine(templateEngine());
    resolver.setCharacterEncoding("UTF-8");
    resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 20);
    return resolver;
}

But navigating to http://localhost:8080 after that configuration reports the following error...

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:958)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:837)

....

@berinle
Copy link
Author

berinle commented Oct 16, 2013

Apparently, this is a bug with thymeleaf 2.0.17 in offline mode.

See:
http://forum.thymeleaf.org/Using-thymeleaf-as-email-templating-engine-with-Spring-Boot-td4026893.html
-- and --
thymeleaf/thymeleaf-spring#26

Bumping the version to 2.0.18 or higher resolved the issue.

Thanks!

PS: It will still be helpful to know how to override the default beans e.g. templateEngine defined by @EnableAutoConfiguration. The docs don't do a good job at explaining it. That was the second part this bug ticket.

@berinle berinle closed this as completed Oct 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant