From dd235b16d38bcbdd67a609c1259802703d9c5364 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 21 Jan 2020 20:07:39 +0100 Subject: [PATCH] WIP --- .../java/de/juplo/demo/DemoController.java | 62 +++++++++---------- src/main/java/de/juplo/demo/Form.java | 36 +++++++++++ src/main/resources/templates/form.html | 35 +++++++++++ 3 files changed, 102 insertions(+), 31 deletions(-) create mode 100644 src/main/java/de/juplo/demo/Form.java create mode 100644 src/main/resources/templates/form.html diff --git a/src/main/java/de/juplo/demo/DemoController.java b/src/main/java/de/juplo/demo/DemoController.java index ddf4e06..25b500b 100644 --- a/src/main/java/de/juplo/demo/DemoController.java +++ b/src/main/java/de/juplo/demo/DemoController.java @@ -23,55 +23,55 @@ public class DemoController @RequestMapping("/") - public String display( - @ModelAttribute("form") Map> form) + public String display(@ModelAttribute Form form) { - for (Integer id : form.keySet()) - for (String entry : form.get(id).keySet()) - LOG.info("{} - {}: {}", id, entry, form.get(id).get(entry)); + for (Integer id : form.cards.keySet()) + for (String entry : form.cards.get(id).keySet()) + LOG.info("{} - {}: {}", id, entry, form.cards.get(id).get(entry)); return "form"; } - @RequestMapping(path = "/", params = "row") - public String addRow( - @ModelAttribute("form") Map> form, - @RequestParam Integer row) + @RequestMapping(path = "/", params = "add=card") + public String addCard(@ModelAttribute Form form) { - LOG.info("Adding row #{}", row); - form.put(row, new HashMap<>()); + Integer next = + form.cards + .keySet() + .stream() + .reduce(0, (a, b) -> a > b ? a : b) + 1; + + LOG.info("Adding new card #{}", next); + form.cards.put(next, new HashMap<>()); return "form"; } - @RequestMapping(path = "/", params = "row") - public String removeRow( - @ModelAttribute("form") Map> form, - @RequestParam Integer row) + @RequestMapping(path = "/", params = "remove=card") + public String removeCard( + @ModelAttribute Form form, + @RequestParam Integer card) { - Map content = form.remove(row); - LOG.info("Removed row #{} with content: {}", row, content); + Map content = form.cards.remove(card); + LOG.info("Removed card #{} with content: {}", card, content); return "form"; } - @RequestMapping(path = "/", params = { "row", "entry" }) - public String addEntry( - @ModelAttribute("form") Map> form, - @RequestParam Integer row, - @RequestParam String entry) + @RequestMapping(path = "/", params = "add=row") + public String addRow(@ModelAttribute Form form, @RequestParam Integer card) { - LOG.info("Adding entry {} to row #{}", entry, row); - form.get(row).put(entry, Boolean.FALSE); + LOG.info("Adding row {} to card #{}", form.row.get(card), card); + form.cards.get(card).put(form.row.get(card), Boolean.FALSE); return "form"; } - @RequestMapping(path = "/", params = { "row", "entry" }) - public String removeEntry( - @ModelAttribute("form") Map> form, - @RequestParam Integer row, - @RequestParam String entry) + @RequestMapping(path = "/", params = "remove!=card") + public String removeRow(@ModelAttribute Form form, @RequestParam String remove) { - Boolean value = form.get(row).remove(entry); - LOG.info("Removed entry {} with value {} from row #{}", entry, value, row); + String[] parts = remove.split(":", 2); + Integer card = Integer.valueOf(parts[0]); + String row = parts[1]; + Boolean value = form.cards.get(card).remove(row); + LOG.info("Removed row {} with value {} from card #{}", row, value, card); return "form"; } } diff --git a/src/main/java/de/juplo/demo/Form.java b/src/main/java/de/juplo/demo/Form.java new file mode 100644 index 0000000..382c335 --- /dev/null +++ b/src/main/java/de/juplo/demo/Form.java @@ -0,0 +1,36 @@ +package de.juplo.demo; + + +import java.util.Map; + + +/** + * + * @author Kai Moritz + */ +public class Form +{ + Map> cards; + Map row; + + + public Map> getCards() + { + return cards; + } + + public void setCards(Map> cards) + { + this.cards = cards; + } + + public Map getRow() + { + return row; + } + + public void setRow(Map row) + { + this.row = row; + } +} diff --git a/src/main/resources/templates/form.html b/src/main/resources/templates/form.html new file mode 100644 index 0000000..586a812 --- /dev/null +++ b/src/main/resources/templates/form.html @@ -0,0 +1,35 @@ + + + + + + Demo: Multidimensional Map With Dynamic Binding + + + + +
+
+
+

Card #X

+
+
+
+ + + +
+ +
+ +
+
+ +
+
+ + -- 2.20.1