Search code examples
spring-mvcspring-bootthymeleaf

Thymeleaf: Thymeleaf template parsing error


I'm implementing password reset functionality. To display the web page with this functionality I'm using thymeleaf. But when I call my simple demo.html page (see code below) it throws me following error:

There was an unexpected error (type=Internal Server Error, status=500).

Exception parsing document: template="demo", line 10 - column

Bean configuration:

@Bean
public ClassLoaderTemplateResolver emailTemplateResolver(){
    ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
    emailTemplateResolver.setPrefix("templates/mail/");
    emailTemplateResolver.setSuffix(".html");
    emailTemplateResolver.setTemplateMode("HTML5");
    emailTemplateResolver.setCharacterEncoding("UTF-8");
    emailTemplateResolver.setOrder(1);
    return emailTemplateResolver;
}

demo.html:

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <form th:action="@{/resetpassword(_key=${resetPasswordToken})}" method="post">
    <div>
    <input type="password" th:field="*{password}">
    <input type="password">
    </div>
    </form>
</body>
</html> 

Controller:

@RequestMapping(value = "/resetpassword", method = RequestMethod.GET)
public String resetpasswordView(@RequestParam(value = "_key") String resetPasswordToken, final Model model) {
    System.out.println(resetPasswordToken);
    model.addAttribute("resetPasswordToken", resetPasswordToken);
    return "demo";
}

What am I missing here?


Solution

  • By default, Thymeleaf requires your templates to be valid XML. You haven't closed the <input> tags so when it reaches </div> on line 10 it fails as the tags are unbalanced.

    You can fix the problem by closing the tag:

    <input type="password" th:field="*{password}" />
    

    Alternatively, you could use Thymeleaf's legacy HTML5 mode but it requires a bit more setup.