I am trying to use Thymeleaf template engine for generating Emails in my Spring Web Application. I have followed the documentation on their website, but I keep getting a SaxParseException no matter how simple my HTML5 template is. I am using a HTML 5 template as shown below.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title th:remove="all">Account confirmation HTML email</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h3 th:utext="#{mail.template.confirmation.greeting(${user.firstName}, ${user.lastName})}"></h3>
<p th:utext="#{mail.template.confirmation.introduction(${createdDate})}">
</p>
</body>
</html>
My Spring config.
@Bean
public ClassLoaderTemplateResolver emailTemplateResolver() {
ClassLoaderTemplateResolver cltr = new ClassLoaderTemplateResolver();
cltr.setPrefix("/dk/rfit/orderit/web/mail/");
cltr.setSuffix(".html");
cltr.setTemplateMode("HTML5");
cltr.setCharacterEncoding("UTF-8");
cltr.setOrder(3);
cltr.setCacheable(true);
return cltr;
}
@Bean
public TemplateEngine mailTemplateEngine() {
SpringTemplateEngine ste = new SpringTemplateEngine();
ste.setTemplateResolver(emailTemplateResolver());
return ste;
}
StackTrace..
2013-01-31 08:01:33,431 [org.thymeleaf.TemplateEngine] [http-bio-8080-exec-10] (TemplateEngine.java:829) INFO org.thymeleaf.TemplateEngine - [THYMELEAF] INITIALIZING TEMPLATE ENGINE
2013-01-31 08:01:33,496 [org.thymeleaf.templateresolver.AbstractTemplateResolver] [http-bio-8080-exec-10] (AbstractTemplateResolver.java:99) INFO org.thymeleaf.templateresolver.AbstractTemplateResolver - [THYMELEAF] INITIALIZING TEMPLATE RESOLVER: org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
2013-01-31 08:01:33,496 [org.thymeleaf.templateresolver.AbstractTemplateResolver] [http-bio-8080-exec-10] (AbstractTemplateResolver.java:110) INFO org.thymeleaf.templateresolver.AbstractTemplateResolver - [THYMELEAF] TEMPLATE RESOLVER INITIALIZED OK
2013-01-31 08:01:33,498 [org.thymeleaf.messageresolver.AbstractMessageResolver] [http-bio-8080-exec-10] (AbstractMessageResolver.java:72) INFO org.thymeleaf.messageresolver.AbstractMessageResolver - [THYMELEAF] INITIALIZING MESSAGE RESOLVER: org.thymeleaf.spring3.messageresolver.SpringMessageResolver
2013-01-31 08:01:33,499 [org.thymeleaf.messageresolver.AbstractMessageResolver] [http-bio-8080-exec-10] (AbstractMessageResolver.java:78) INFO org.thymeleaf.messageresolver.AbstractMessageResolver - [THYMELEAF] MESSAGE RESOLVER INITIALIZED OK
2013-01-31 08:01:33,506 [org.thymeleaf.TemplateEngine.CONFIG] [http-bio-8080-exec-10] (ConfigurationPrinterHelper.java:130) INFO org.thymeleaf.TemplateEngine.CONFIG - [THYMELEAF] TEMPLATE ENGINE CONFIGURATION:
[THYMELEAF] * Cache Factory implementation: org.thymeleaf.cache.StandardCacheManager
[THYMELEAF] * Template modes:
[THYMELEAF] * LEGACYHTML5
[THYMELEAF] * VALIDXHTML
[THYMELEAF] * HTML5
[THYMELEAF] * XHTML
[THYMELEAF] * XML
[THYMELEAF] * VALIDXML
[THYMELEAF] * Template resolvers (in order):
[THYMELEAF] * [3] org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
[THYMELEAF] * Message resolvers (in order):
[THYMELEAF] * org.thymeleaf.spring3.messageresolver.SpringMessageResolver
[THYMELEAF] * Dialect: org.thymeleaf.spring3.dialect.SpringStandardDialect
[THYMELEAF] * Prefix: "th"
[THYMELEAF] TEMPLATE ENGINE CONFIGURED OK
2013-01-31 08:01:33,507 [org.thymeleaf.TemplateEngine] [http-bio-8080-exec-10] (TemplateEngine.java:842) INFO org.thymeleaf.TemplateEngine - [THYMELEAF] TEMPLATE ENGINE INITIALIZED
2013-01-31 08:09:52,146 [org.thymeleaf.TemplateEngine] [http-bio-8080-exec-10] (TemplateEngine.java:1173) ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][http-bio-8080-exec-10] Exception processing template "account-confirmation": Exception parsing document
2013-01-31 08:09:52,149 [dk.rfit.orderit.web.controller.AccountController] [http-bio-8080- exec-10] (AccountController.java:213) ERROR dk.rfit.orderit.web.controller.AccountController org.thymeleaf.templateparser.xmlsax.AbstractNonValidatingSAXTemplateParser.parseTemplateUsingP ool(AbstractNonValidatingSAXTemplateParser.java:167) org.thymeleaf.templateparser.xmlsax.AbstractNonValidatingSAXTemplateParser.parseTemplate(Abstr actNonValidatingSAXTemplateParser.java:115)
at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:276)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1192)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1148)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1095)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1008)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:982)
dk.rfit.orderit.web.service.EmailServiceThymeLeafImpl$1.prepare(EmailServiceThymeLeafImpl.java:65)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:352)
... 73 more
Caused by: org.xml.sax.SAXException: Scanner State 24 not Recognized
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1245 )
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:5 22)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:395)
at org.thymeleaf.templateparser.xmlsax.AbstractNonValidatingSAXTemplateParser.doParse(AbstractNon ValidatingSAXTemplateParser.java:208)
at org.thymeleaf.templateparser.xmlsax.AbstractNonValidatingSAXTemplateParser.parseTemplateUsingP ool(AbstractNonValidatingSAXTemplateParser.java:132)
... 82 more
Hope that somebody can tell me what to do to solve this problem..
In addition to Vel's answer: if you need to insert invalid XHTML via utext, which could be perfectly fine for HTML5 (like unclosed <br> tags), you need to use template mode LEGACYHTML5:
cltr.setTemplateMode("LEGACYHTML5");