Thymeleaf - Strict HTML parsing issue

Here is how you can do it in a neat way

Step 1: Add thymeleaf mode to your application.properties file.

resources/application.properties

spring.thymeleaf.mode=LEGACYHTML5

Step 2: Add nekohtml dependency to your pom.xml file.

pom.xml

<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
</dependency>

All you have to do is run Thymeleaf in "LEGACYHTML5" mode and it works like a charm. Thanks to this and this post, I found the solution and am documenting in SO so others do not have to go through the same trouble in finding this answer.

To set the legacy mode you can define the bean in your Spring XML file:

<!-- View TemplateResolver -->
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="templateMode" value="LEGACYHTML5"/>
    <property name="cacheable" value="false"/>
</bean>

or add the properties to the application.properties file:

spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false

And in both cases you have to add the nekohtml jar to your project or, if you are running maven, you can add its dependency to your pom.xml

<dependency>
     <groupId>net.sourceforge.nekohtml</groupId>
     <artifactId>nekohtml</artifactId>
     <version>1.9.21</version>
 </dependency>

Gradle

'net.sourceforge.nekohtml:nekohtml:1.9.21'

Tags:

Html

Thymeleaf