![](/img/trans.png)
[英]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.