BAEL-5648: Thymeleaf - How to add checked attribute to input conditio… (#14180)

* BAEL-5648: Thymeleaf - How to add checked attribute to input conditionally

* BAEL-5648: Thymeleaf - How to add checked attribute to input conditionally
This commit is contained in:
ACHRAF TAITAI 2023-06-05 07:59:40 +02:00 committed by GitHub
parent 104df66ef8
commit c341505b45
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package com.baeldung.thymeleaf.attribute;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class CheckedAttributeController {
@GetMapping("/checked")
public String displayCheckboxForm(Model model) {
Engine engine = new Engine(true);
model.addAttribute("engine", engine);
model.addAttribute("flag", true);
return "attribute/index";
}
private static class Engine {
private Boolean active;
public Engine(Boolean active) {
this.active = active;
}
public Boolean getActive() {
return active;
}
}
}

View File

@ -0,0 +1,27 @@
<!DOCTYPE HTML>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Spring Boot Thymeleaf Application - Checkbox Checked Conditionally</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col">
<form method="post">
<label>
<input type="checkbox" th:checked="${flag}"/> Flag activated
</label>
<label>
<input type="checkbox" th:checked="${engine.getActive()}"/> Customer activated
</label>
<label>
<input type="checkbox" th:checked="${flag ? false: true}"/> Flag deactivated
</label>
</form>
</div>
</div>
</div>
</body>
</html>