0

View(design.html): https://github.com/habuma/spring-in-action-6-samples/blob/main/ch05/taco-cloud/src/main/resources/templates/design.html

... <form th:method="POST" th:object="${taco}" th:action="@{/design}" id="tacoForm"> <span class="validationError" th:if="${#fields.hasErrors('ingredients')}" th:errors="*{ingredients}">Ingredient Error</span> ... 

Controller: https://github.com/habuma/spring-in-action-6-samples/blob/main/ch05/taco-cloud/src/main/java/tacos/web/DesignTacoController.java

public class DesignTacoController { ... @ModelAttribute(name = "taco") public Taco taco() { return new Taco(); } @GetMapping public String showDesignForm() { return "design"; } ... @PostMapping public String processTaco( @Valid Taco taco, Errors errors, @ModelAttribute TacoOrder order) { log.info(" --- Saving taco"); if (errors.hasErrors()) { return "design"; } Taco saved = tacoRepo.save(taco); order.addTaco(saved); return "redirect:/orders/current"; } } 

As i know, a new Taco object named taco will be created and added to the model due to @ModelAttribute(name = "taco") annotation before the method. This is fine because we need a empty Taco object for the GET /design request(showDesignForm). But when we submit the form, a fresh new taco IS AGAIN created & added to the model and because we do NOT add @ModelAttribute to the processTaco's taco parameter(@Valid Taco taco), the view will get a fresh new taco which is not populated by the submitted form data. So in this case, the view was not supposed to display the current/changed taco info(i.e. changed fields or error info) correctly. But i tested and found it works.

I've search a lot about @ModelAttribute and could not find any answer to the above confusion.

1 Answer 1

0

For the PostMapping, spring framework automatically creates the taco object from the submitted form data, validates it and then supplies to the processTaco method. Did you actually debug and see if there is no value in the taco object supplied to processTaco method?

Sign up to request clarification or add additional context in comments.

1 Comment

The taco object in processTaco was populated correctly from form data. What i confused about was the view would see a refresh new taco every time, so the view would not render the current taco info correctly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.