Added checkboxes, that are stored in a map
authorKai Moritz <kai@juplo.de>
Tue, 21 Jan 2020 22:48:25 +0000 (23:48 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 7 Apr 2023 11:15:40 +0000 (13:15 +0200)
src/main/java/de/juplo/demo/DemoController.java
src/main/java/de/juplo/demo/Form.java
src/main/resources/templates/form.html

index 3f8d0dd..b841780 100644 (file)
@@ -1,6 +1,7 @@
 package de.juplo.demo;
 
 
+import java.util.stream.Collectors;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.ModelAttribute;
@@ -18,7 +19,15 @@ public class DemoController
   @RequestMapping("/")
   public String display(@ModelAttribute Form form)
   {
-    log.info("option={}, inner={}", form.option, form.inner.option);
+    log.info(
+        "option={}, inner={}{}",
+        form.option,
+        form.inner.option,
+        form.map
+            .entrySet()
+            .stream()
+            .map(entry -> entry.getKey() + "=" + entry.getValue())
+            .collect(Collectors.joining(", ", ", ", "")));
     return "form";
   }
 }
index e7a40af..c44eb6c 100644 (file)
@@ -1,6 +1,9 @@
 package de.juplo.demo;
 
 
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import lombok.Data;
 
 
@@ -13,6 +16,8 @@ public class Form
 {
   Boolean option;
   Inner inner = new Inner();
+  Map<String, Boolean> map =
+      Stream.of( "foo", "bar" ).collect(Collectors.toMap(a -> a, a -> false));
 }
 
 @Data
index 10ebfaa..533758e 100644 (file)
                 <input type="checkbox" class="form-check-label" th:field="*{inner.option}" />
                 <label class="form-check-label" th:for="${#ids.prev('inner.option')}">Another Option (In An Inner Class)</label>
               </div>
+              <div class="form-group" th:each="key : *{map.keySet()}">
+                <input type="checkbox" class="form-check-label" th:field="*{map[__${key}__]}" />
+                <!-- <label class="form-check-label" th:for="${#ids.prev('map[__${key}__]')}" th:text='|Option "${key}" From The Map|'>Option "KEY" From The Map</label>-->
+                <label class="form-check-label" th:text='|Option "${key}" From The Map|'>Option "KEY" From The Map</label>
+              </div>
             </div>
           </div>
           <div class="card-footer">