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;
/**
.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);
+ }
}
package de.juplo.demo;
+import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Boolean option;
Inner inner = new Inner();
Map<String, Boolean> 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
<input type="checkbox" class="form-check-label" th:field="*{map[__${key}__]}" />
<label class="form-check-label" th:for="|map${key}1|" th:text='|Option "${key}" From The Map|'>Option "KEY" From The Map</label>
</div>
+ <div class="input-group">
+ <span class="input-group-prepend"><span class="input-group-text">New Option</span></span>
+ <input type="input" class="form-control" name="name" />
+ <span class="input-group-append"><button type="submit" class="btn btn-primary" name="add">Add</button></span>
+ </div>
</div>
</div>
<div class="card-footer">