Как в Spring правильно принимать данные с checkbox?
Я пытался следующий вариант:
Controller.java:
@Controller
@RequestMapping("/test")
public class TestController {
@GetMapping
public ModelAndView getTest(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("test");
boolean confirm1 = false;
modelAndView.addObject("confirm1", confirm1);
return modelAndView;
}
@PostMapping
public ModelAndView postDeleteAccount(
@ModelAttribute("confirm1") boolean confirm1,
BindingResult bindingResult
){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("test");
if(!confirm1){
bindingResult
.rejectValue("confirm1", "error.confirm1",
"*You must confirm these conditions."
);
}
return modelAndView;
}
}
Test.html:
<label th:for="confirm1">Тест действия checkbox</label>
<input type="checkbox" th:field="*{confirm1}" />