如何解决Thymeleaf的TemplateInputException及响应消息不显示问题
账户解锁功能异常排查
点击邮件链接进入账户解锁页面,输入临时密码和新密码提交后,数据已成功存入数据库,但UI无法显示成功/错误提示,同时抛出org.thymeleaf.exceptions.TemplateInputException异常。
出错的unlock.html代码
<p th:text="${successMsg}" class="text-success"> <p th:text="${errorMsg}" class="text-danger"> <form th:action="@{/unlock}" th:object="${unlock}" th:method="POST"> <div class="form-outline mb-6"> <h6>Your Email :<p th:text="${unlock.email}"/> </h6> <input type="hidden" th:field="${unlock.email}"/> </div> <div class="form-outline mb-4"> <input type="password" id="form2Example22" class="form-control" th:field="*{tempPwd}"/> <label class="form-label" for="form2Example22">Temporary Password</label> </div> <div class="form-outline mb-4"> <input type="password" id="form2Example23" class="form-control" th:field="*{newPwd}"/> <label class="form-label" for="form2Example23">New Password</label> </div> <div class="form-outline mb-4"> <input type="password" id="form2Example24" class="form-control" th:field="*{confPwd}"/> <label class="form-label" for="form2Example24">Confirm Password</label> </div> <div class="text-center pt-1 mb-5 pb-1"> <input type="submit" value="unlock" class="btn btn-primary btn-block fa-lg gradient-custom-2 mb-3"/> </div> </form>
Controller代码
@GetMapping("/unlock") public String unlockPage(@RequestParam String email, Model model) { UnlockForm unlockFormObj = new UnlockForm(); unlockFormObj.setEmail(email); model.addAttribute("unlock",unlockFormObj); return "unlock"; } @PostMapping("/unlock") public String unlockUserAccount(@ModelAttribute("unlock)") UnlockForm unlock, Model model) { //validate user entered password and current password should same if (unlock.getNewPwd().equals(unlock.getConfPwd())){ //if valid then unlock the account boolean status = studentService.unlockAccount(unlock); if (status){ model.addAttribute("successMsg", "Your account unlocked successfully.."); }else { model.addAttribute("errorMsg","Given temporary password is incorrect, check email once.."); } }else { //if not valid then send error msg as response to the client model.addAttribute("errMsg", "New Password and Confirm password must be same"); } System.out.println(unlock); return "unlock"; }
异常堆栈信息
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/unlock.html]") org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666) org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:366) org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:190) org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1406) org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1150) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:965) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) javax.servlet.http.HttpServlet.service(HttpServlet.java:555) org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) javax.servlet.http.HttpServlet.service(HttpServlet.java:623) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
问题排查点
- HTML模板中
<p>标签未闭合,导致Thymeleaf解析失败:页面开头的两个<p>标签未闭合,破坏HTML结构,引发模板解析异常。 - Controller中
@ModelAttribute("unlock)")存在引号语法错误:属性值引号不匹配,应为@ModelAttribute("unlock"),语法错误会导致参数绑定异常。 - UI中使用的
errorMsg与Controller中设置的errMsg变量名不一致:新密码和确认密码不匹配时,Controller存入的是errMsg,但页面使用errorMsg,导致该错误提示无法显示。
内容的提问来源于stack exchange,提问作者Pramod Lad
相关产品推荐
相关产品推荐

