c74a6e99238f2c180d0b60fa70905abc6eb996cf
[demos/spring-boot] / src / main / java / de / juplo / demo / DemoController.java
1 package de.juplo.demo;
2
3
4 import java.util.stream.Collectors;
5 import lombok.extern.slf4j.Slf4j;
6 import org.springframework.stereotype.Controller;
7 import org.springframework.web.bind.annotation.ModelAttribute;
8 import org.springframework.web.bind.annotation.RequestMapping;
9 import org.springframework.web.bind.annotation.RequestParam;
10 import org.thymeleaf.util.StringUtils;
11
12
13 /**
14  * Controller to demonstrate the behavior of checkboxes
15  * @author Kai Moritz
16  */
17 @Controller
18 @Slf4j
19 public class DemoController
20 {
21   @RequestMapping("/")
22   public String display(@ModelAttribute Form form)
23   {
24     log.info(
25         "option={}, inner={}{}",
26         form.option,
27         form.inner.option,
28         form.map
29             .entrySet()
30             .stream()
31             .map(entry -> entry.getKey() + "=" + entry.getValue())
32             .collect(Collectors.joining(", ", ", ", "")));
33     return "form";
34   }
35
36   @RequestMapping(path = "/", params = "add")
37   public String add(@ModelAttribute Form form, @RequestParam String name)
38   {
39     if (!StringUtils.isEmptyOrWhitespace(name))
40     {
41       form.map.put(name.trim(), Boolean.FALSE);
42       log.info("Added option \"{}\" to the map", name.trim());
43     }
44     else
45     {
46       log.info("Ignoring empty option-name");
47     }
48     return display(form);
49   }
50 }