0

I am using Spring Boot 2.4.1 with Thymeleaf, and OpenJDK 11.0.9.1 on Ubuntu 20.10. I have tagged the entities with @NotNull and other tags, and the @Valid tag seems to be working fine in the controller's method that handles POST requests, because on error it returns me to the original page. However, th:if...th:errors in the Thymeleaf template are not working to show the corresponding error message.

The Thymeleaf template (/editor) includes a form:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<body>
    ...
    <form method="POST" th:object="${competence}">
            <h3>Additional information</h3>
            <label for="compname">Name:</label>
            <input type="text" id="compname" th:field="*{name}" />
            <span class="validationError"
                  th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Error</span><br/>
            <label for="compdesc">Description:</label>
            <textarea class="description" id="compdesc" th:field="*{description}" />                
            <button>Regístrala</button>
        </div>
    </form>

Whereas the controller includes a method for handling POST requests:

@Slf4j
@Controller
@RequestMapping("/editor")
public class CompetenceEditorController {
...

    @PostMapping
    public String processCompetence(@Valid CompetenceEntity competence,
            final BindingResult bindingResult, final Model model) {

        // Checking for errors in constraints defined in the class CompetenceEntity.
        if (bindingResult.hasErrors()) {
            return "editor";
        } else {    
            // Save the competence
            log.info("Processing competence: " + competence);
            CompetenceEntity saved = competenceRepository.save(competence);
            return "redirect:/competence-saved";
        }
    }

If I fill name and description, the process continues, but if I leave name empty, the code sends me back to the /editor page, but there is no error message. The span element dissappears before and after the error.

EDIT

I thought that the suggestion by @dirk-deyne worked, but only does so if (1) I run the Spring Boot on the previous code, (2) I leave name blank (and I get back to the same page without error message), (3) I get Boot down, "fix" the code, and run it again, (4) leave name blank again, and again, and again.

But if I leave the page, and then I get back again, I get the error message:

There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/editor.html]")
...
Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "editor" - line 51, col 46)

Where line 51 is

            <input type="text" id="compname" th:field="*{name}" />

And column 46 (including spaces) is the start of th:field. By the way, the @GetMapping method has the following line

        model.addAttribute("competence", new CompetenceEntity());

and that is why I was using th:object="${competence}" instead of th:object="${competenceEntity}".

  • 1
    Are you sure your form is submitted? It is missing `th:action="@{/editor}"` – Dirk Deyne Jan 13 '21 at 15:26
  • Yes, the form is submitted when the constraint of @NotBlank on name is met, as described in Craig Walls' book "Spring in Action". – Rafael Morales-Gamboa Jan 13 '21 at 16:36
  • You should use `competenceEntity` instead of `competence` in your form ref this [answer](https://stackoverflow.com/questions/22948257/thymeleaf-not-displaying-spring-form-error-messages?rq=1) – Dirk Deyne Jan 13 '21 at 20:26

1 Answers1

1

Well, the solution was were Dirk Deyne said. The problem got fixed by linking the competence in Thymeleaf with the competence in the @PostMapping method using @ModelAttribute("competence"):

    @PostMapping
    public String processCompetence(
            @Valid @ModelAttribute("competence") CompetenceEntity competence,
            final BindingResult bindingResult, final Model model) {

Thank you Dirk.