Issue
i need to use check box to fire off a java if statement without having to bind the check box to any entity or object here is what i need in code
<input type="checkbox" th:field="${deleteImages}" th:checked="*{false}">Delete all images</input>
the controller would be this
public String updateProduct(@ModelAttribute("product") Product product,
@ModelAttribute("deleteImages") Boolean deleteImages) {
if (deleteImages) {
imageService.deleteImages(savedProduct.getId());
}
i can not figure out how to fix this checkbox.. i don't need it tied up to any object just return true when clicked and false when unchecked
Solution
If you want to do this without binding it to an object, just use RequestParams. Like this:
Form:
<form method="POST" class="action-buttons-fixed">
<input type="checkbox" name="deleteImages" />
<button>Go</button>
</form>
Java:
@PostMapping("/whatever")
public String updateProduct(@RequestParam(required = false, defaultValue = "false") boolean deleteImages) {
if (deleteImages) {
imageService.deleteImages(savedProduct.getId());
}
}
You can't, however, mix bound and unbound properties on the same form. If you want to do that, you should just create a new Form
object that contains the both the product and the deleteImages variable.
class Form {
private Product product;
private boolean deleteImages;
}
And then change your form bindings appropriately.
Answered By - Metroids
Answer Checked By - Pedro (JavaFixing Volunteer)