[英]Null Objects after submitting Form in Spring (H2, SpringBoot, Thymeleaf, CRUD)
[英]submitting checkboxes with Thymeleaf + SpringBoot
我有一個 SpringBoot 應用程序。 使用這個 thymelaf 模板,在提交時可以正常工作:
<div class="form-group required-control">
<label for="gre">GRE</label>
<input id="gre" type="checkbox" name="gre" th:checked="*{gre}" th:onclick="submit()" />
</div>
但是當我添加另一個復選框時,它總是考慮第一個,無論我點擊哪個
<div class="form-group required-control">
<label for="gre">GRE</label>
<input id="gre" type="checkbox" name="gre" th:checked="*{gre}" th:onclick="submit()" />
<label for="gre2">GRE2</label>
<input id="gre2" type="checkbox" name="gre2" th:checked="*{gre2}" th:onclick="submit()" />
</div>
這里沒有技術問題。 我認為您的submit()
function 存在問題,因為我創建了一個普通表單並嘗試了您的相同實例,並且所有選擇組合都正常工作。
例如,我分別添加了實體 controller 和 html 文件。
public class Example {
private boolean gre;
private boolean gre2;
public Example() {
}
// getter/setter ...
}
@Controller
@RequestMapping("/example")
public class ExampleController {
@GetMapping("/create")
public String createExample(Model model) {
model.addAttribute("example", new Example());
return "example-form";
}
@PostMapping("/insert")
public String insertExample(Model model, Example example) {
model.addAttribute("example", example);
return "example-form";
}
}
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<div>
<form action="/example/insert" method="post" th:object="${example}">
<div class="form-group required-control">
<label for="gre">GRE</label>
<input id="gre" type="checkbox" name="gre" th:checked="*{gre}" />
<label for="gre2">GRE 2</label>
<input id="gre2" type="checkbox" name="gre2" th:checked="*{gre2}" />
</div>
<button type="submit">Submit Form</button>
</form>
</div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.