From 09f62ffe687ad4a58ae5cc82c4671102f7f53562 Mon Sep 17 00:00:00 2001 From: Amy DeGregorio Date: Tue, 25 Jun 2019 08:57:18 -0400 Subject: [PATCH] BAEL-3015 (#7189) --- spring-thymeleaf-2/pom.xml | 40 +++++++++++++++++++ .../com/baeldung/thymeleaf/Application.java | 11 +++++ .../com/baeldung/thymeleaf/enums/Color.java | 22 ++++++++++ .../com/baeldung/thymeleaf/enums/Widget.java | 27 +++++++++++++ .../thymeleaf/enums/WidgetController.java | 23 +++++++++++ .../main/resources/templates/enums/new.html | 19 +++++++++ .../main/resources/templates/enums/view.html | 30 ++++++++++++++ 7 files changed, 172 insertions(+) create mode 100644 spring-thymeleaf-2/pom.xml create mode 100644 spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/Application.java create mode 100644 spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/Color.java create mode 100644 spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/Widget.java create mode 100644 spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/WidgetController.java create mode 100644 spring-thymeleaf-2/src/main/resources/templates/enums/new.html create mode 100644 spring-thymeleaf-2/src/main/resources/templates/enums/view.html diff --git a/spring-thymeleaf-2/pom.xml b/spring-thymeleaf-2/pom.xml new file mode 100644 index 0000000000..bc1147cb14 --- /dev/null +++ b/spring-thymeleaf-2/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + spring-thymeleaf-2 + spring-thymeleaf-2 + war + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + + 1.8 + 1.8 + + + + + + org.apache.maven.plugins + maven-war-plugin + + + spring-thymeleaf-2 + + diff --git a/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/Application.java b/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/Application.java new file mode 100644 index 0000000000..2ccca82497 --- /dev/null +++ b/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/Application.java @@ -0,0 +1,11 @@ +package com.baeldung.thymeleaf; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/Color.java b/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/Color.java new file mode 100644 index 0000000000..9e223d1323 --- /dev/null +++ b/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/Color.java @@ -0,0 +1,22 @@ +package com.baeldung.thymeleaf.enums; + +public enum Color { + BLACK("Black"), + BLUE("Blue"), + RED("Red"), + YELLOW("Yellow"), + GREEN("Green"), + ORANGE("Orange"), + PURPLE("Purple"), + WHITE("White"); + + private final String displayValue; + + private Color(String displayValue) { + this.displayValue = displayValue; + } + + public String getDisplayValue() { + return displayValue; + } +} diff --git a/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/Widget.java b/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/Widget.java new file mode 100644 index 0000000000..dc6504c3bc --- /dev/null +++ b/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/Widget.java @@ -0,0 +1,27 @@ +package com.baeldung.thymeleaf.enums; + +public class Widget { + private String name; + private Color color; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } + + @Override + public String toString() { + return "Widget [name=" + name + ", color=" + color + "]"; + } +} diff --git a/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/WidgetController.java b/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/WidgetController.java new file mode 100644 index 0000000000..c66464d75b --- /dev/null +++ b/spring-thymeleaf-2/src/main/java/com/baeldung/thymeleaf/enums/WidgetController.java @@ -0,0 +1,23 @@ +package com.baeldung.thymeleaf.enums; + +import javax.validation.Valid; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.ui.Model; + +@Controller +public class WidgetController { + @GetMapping("/widget/add") + public String addWidget(@ModelAttribute Widget widget) { + return "enums/new"; + } + + @PostMapping("/widget/add") + public String saveWidget(@Valid Widget widget, Model model) { + model.addAttribute("widget", widget); + return "enums/view"; + } +} diff --git a/spring-thymeleaf-2/src/main/resources/templates/enums/new.html b/spring-thymeleaf-2/src/main/resources/templates/enums/new.html new file mode 100644 index 0000000000..7ac4665171 --- /dev/null +++ b/spring-thymeleaf-2/src/main/resources/templates/enums/new.html @@ -0,0 +1,19 @@ + + + + +Enums in Thymeleaf + + +
+

Add New Widget

+ + + + + +
+ + \ No newline at end of file diff --git a/spring-thymeleaf-2/src/main/resources/templates/enums/view.html b/spring-thymeleaf-2/src/main/resources/templates/enums/view.html new file mode 100644 index 0000000000..5f473886d6 --- /dev/null +++ b/spring-thymeleaf-2/src/main/resources/templates/enums/view.html @@ -0,0 +1,30 @@ + + + + +Enums in Thymeleaf + + +

View Widget

+
+ + +
+
+ + +
+
+ This color screams danger. +
+
+ Green is for go. +
+
+ Alert + Warning + Caution + All Good +
+ + \ No newline at end of file