From: Kai Moritz Date: Tue, 21 Jan 2020 23:38:43 +0000 (+0100) Subject: Additionally options can be added dynamically X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=bc4a7fe4decdbbb69186ef203593346816de7e2f;p=demos%2Fspring-boot Additionally options can be added dynamically --- diff --git a/src/main/java/de/juplo/demo/DemoController.java b/src/main/java/de/juplo/demo/DemoController.java index b841780..c74a6e9 100644 --- a/src/main/java/de/juplo/demo/DemoController.java +++ b/src/main/java/de/juplo/demo/DemoController.java @@ -6,6 +6,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.thymeleaf.util.StringUtils; /** @@ -30,4 +32,19 @@ public class DemoController .collect(Collectors.joining(", ", ", ", ""))); return "form"; } + + @RequestMapping(path = "/", params = "add") + public String add(@ModelAttribute Form form, @RequestParam String name) + { + if (!StringUtils.isEmptyOrWhitespace(name)) + { + form.map.put(name.trim(), Boolean.FALSE); + log.info("Added option \"{}\" to the map", name.trim()); + } + else + { + log.info("Ignoring empty option-name"); + } + return display(form); + } } diff --git a/src/main/java/de/juplo/demo/Form.java b/src/main/java/de/juplo/demo/Form.java index c44eb6c..b6e933b 100644 --- a/src/main/java/de/juplo/demo/Form.java +++ b/src/main/java/de/juplo/demo/Form.java @@ -1,6 +1,7 @@ package de.juplo.demo; +import java.util.LinkedHashMap; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -17,7 +18,8 @@ public class Form Boolean option; Inner inner = new Inner(); Map map = - Stream.of( "foo", "bar" ).collect(Collectors.toMap(a -> a, a -> false)); + new LinkedHashMap<>( + Stream.of( "foo", "bar" ).collect(Collectors.toMap(a -> a, a -> false))); } @Data diff --git a/src/main/resources/templates/form.html b/src/main/resources/templates/form.html index b8b5158..70841c9 100644 --- a/src/main/resources/templates/form.html +++ b/src/main/resources/templates/form.html @@ -28,6 +28,11 @@ +
+ New Option + + +