4 import java.util.LinkedHashMap;
5 import java.util.stream.Collectors;
6 import java.util.stream.Stream;
7 import lombok.extern.slf4j.Slf4j;
8 import org.springframework.stereotype.Controller;
9 import org.springframework.web.bind.annotation.ModelAttribute;
10 import org.springframework.web.bind.annotation.RequestMapping;
11 import org.springframework.web.bind.annotation.RequestParam;
12 import org.thymeleaf.util.StringUtils;
16 * Controller to demonstrate the behavior of checkboxes
21 public class DemoController
24 public Form createForm(
25 @RequestParam(name = "name", required = false) String param)
27 Form form = new Form();
34 .collect(Collectors.toMap(a -> a, a -> false)));
40 public String display(@ModelAttribute Form form)
43 "option={}, inner={}{}",
49 .map(entry -> entry.getKey() + "=" + entry.getValue())
50 .collect(Collectors.joining(", ", ", ", "")));
54 @RequestMapping(path = "/", params = "add")
55 public String add(@ModelAttribute Form form, @RequestParam String name)
57 if (!StringUtils.isEmptyOrWhitespace(name))
59 form.map.put(name.trim(), Boolean.FALSE);
60 log.info("Added option \"{}\" to the map", name.trim());
64 log.info("Ignoring empty option-name");
69 @RequestMapping(path = "/", params = "remove")
70 public String remove(@ModelAttribute Form form, @RequestParam String remove)
72 Boolean value = form.map.remove(remove);
73 log.info("Removed option \"{}\" with value {} from the map", remove, value);