Thymeleaf template is unable to processed and throwing NoSuchMethodError

81 Views Asked by At

I'm encountering an issue with the th:href attribute in my Thymeleaf template. I'm using Thymeleaf version 3.1.2.RELEASE along with the Thymeleaf Spring5 integration. Despite setting the th:href attribute correctly, the link is not being generated as expected and throwing below error :

Caused by: java.lang.NoSuchMethodError: 'java.lang.String org.thymeleaf.util.ExpressionUtils.normalize(java.lang.String)'

Below is my setup :

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
        </dependency>
@Configuration
public class ThymeleafTemplateConfig
{
    @Bean
    public SpringTemplateEngine springTemplateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addTemplateResolver(htmlTemplateResolver());
        return templateEngine;
    }

    @Bean
    public ClassLoaderTemplateResolver htmlTemplateResolver(){
        ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
        emailTemplateResolver.setPrefix("templates/");
        emailTemplateResolver.setSuffix(".html");
        emailTemplateResolver.setTemplateMode(TemplateMode.HTML);
        emailTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
        return emailTemplateResolver;
    }
}
@Service
@RequiredArgsConstructor
public class EmailService {

    private final SpringTemplateEngine templateEngine;

    protected String createSummaryEmailBody(RunEntity runEntity, String filesLink) {
        Context context = new Context();
        context.setVariable("year", runEntity.getFinancialYear());
        context.setVariable("period", runEntity.getFinancialPeriod());
        context.setVariable("filesLink", filesLink);
        return templateEngine.process("summary-report", context);
    }
}

summary-report.html :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Report Summary</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        .link {
            color: #007bff;
            text-decoration: none;
        }
    </style>
</head>
<body>
<div>
    <h1>Report Summary</h1>
    <p><strong>Year:</strong> <span th:text="${year}"></span></p>
    <p><strong>Period:</strong> <span th:text="${period}"></span></p>
    <p>Files are ready to view <a class="link" th:href="${filesLink}" target="_blank">here</a>.</p>
</div>
</body>
</html>

In my Java code, I'm setting the filesLink variable in the Thymeleaf context. However, when the template is rendered, getting the above error. I've tried troubleshooting this issue by removing the th:href and then the error stopped.

Could someone please advise on why the th:href attribute might not be working as expected? Any insights or suggestions for resolving this issue would be greatly appreciated. Thank you!

1

There are 1 best solutions below

1
Denis Starzhevskiy On

I've changed thymeleaf-spring5:3.1.2.RELEASE to thymeleaf:3.1.2.RELEASE library because I hadn't found any solution for that problem. And it now works as expected.