Take a look at the example Spring provides for handling form submissions:
https://github.com/spring-guides/gs-handling-form-submission/tree/main/complete
It’s a basic example of a Spring MVC controller that handles forms. Let’s look at the controller class:
@Controller
public class GreetingController {
@GetMapping("/greeting")
public String greetingForm(Model model) {
model.addAttribute("greeting", new Greeting());
return "greeting";
}
@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) {
model.addAttribute("greeting", greeting);
return "result";
}
}
So first the user browses to GET /greeting
, which returns a HTML form, which is then submitted to POST /greeting
.
My question is, why do we add a Greeting object to the model in the @GetMapping
method? The form is an empty form, it doesn’t do anything with this model object?