From dd839be0fd694b9f68189ef3dd8a72f7883c424b Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 22 Jan 2020 10:30:57 +0100 Subject: [PATCH] The default-options "foo" and "bar" are only added for new requests * Otherwise, these options are always automatically readded immediately, when they are removed * A request is considered as new, if it contains the parameter "name", which always exists, if a submit is triggered --- .../java/de/juplo/demo/DemoController.java | 18 ++++++++++++++++++ src/main/java/de/juplo/demo/Form.java | 7 +------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/juplo/demo/DemoController.java b/src/main/java/de/juplo/demo/DemoController.java index a9391db..52fc4fe 100644 --- a/src/main/java/de/juplo/demo/DemoController.java +++ b/src/main/java/de/juplo/demo/DemoController.java @@ -1,7 +1,9 @@ package de.juplo.demo; +import java.util.LinkedHashMap; import java.util.stream.Collectors; +import java.util.stream.Stream; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; @@ -18,6 +20,22 @@ import org.thymeleaf.util.StringUtils; @Slf4j public class DemoController { + @ModelAttribute + public Form createForm( + @RequestParam(name = "name", required = false) String param) + { + Form form = new Form(); + if (param == null) + { + form.map = + new LinkedHashMap<>( + Stream + .of( "foo", "bar" ) + .collect(Collectors.toMap(a -> a, a -> false))); + } + return form; + } + @RequestMapping("/") public String display(@ModelAttribute Form form) { diff --git a/src/main/java/de/juplo/demo/Form.java b/src/main/java/de/juplo/demo/Form.java index b6e933b..02fcd78 100644 --- a/src/main/java/de/juplo/demo/Form.java +++ b/src/main/java/de/juplo/demo/Form.java @@ -1,10 +1,7 @@ package de.juplo.demo; -import java.util.LinkedHashMap; import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.Stream; import lombok.Data; @@ -17,9 +14,7 @@ public class Form { Boolean option; Inner inner = new Inner(); - Map map = - new LinkedHashMap<>( - Stream.of( "foo", "bar" ).collect(Collectors.toMap(a -> a, a -> false))); + Map map; } @Data -- 2.20.1