4 import java.util.stream.Collectors;
5 import java.util.stream.Stream;
6 import lombok.extern.slf4j.Slf4j;
7 import org.springframework.stereotype.Controller;
8 import org.springframework.web.bind.annotation.ModelAttribute;
9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestParam;
11 import org.thymeleaf.util.StringUtils;
15 * Controller to demonstrate the behavior of checkboxes
20 public class DemoController
23 public Form createForm(
24 @RequestParam(name = "name", required = false) String param)
26 Form form = new Form();
31 .forEach(option -> form.map.put(option, false));
37 public String display(@ModelAttribute Form form)
40 "option={}, inner={}{}",
46 .map(entry -> entry.getKey() + "=" + entry.getValue())
47 .collect(Collectors.joining(", ", ", ", "")));
51 @RequestMapping(path = "/", params = "add")
52 public String add(@ModelAttribute Form form, @RequestParam String name)
54 if (!StringUtils.isEmptyOrWhitespace(name))
56 form.map.put(name.trim(), Boolean.FALSE);
57 log.info("Added option \"{}\" to the map", name.trim());
61 log.info("Ignoring empty option-name");
66 @RequestMapping(path = "/", params = "remove")
67 public String remove(@ModelAttribute Form form, @RequestParam String remove)
69 Boolean value = form.map.remove(remove);
70 log.info("Removed option \"{}\" with value {} from the map", remove, value);