From 1627bfee4662676ee03a28c7c5bd1b8c05d527ec Mon Sep 17 00:00:00 2001 From: Michael Olayemi Date: Mon, 21 Nov 2022 04:05:19 +0000 Subject: [PATCH] simple-thymeleaf-expression (#12981) * simple-thymeleaf-expression * expression-update --- .../baeldung/thymeleaf/expression/Dino.java | 51 +++++++++++++++++++ .../thymeleaf/expression/DinoController.java | 46 +++++++++++++++++ .../src/main/resources/messages.properties | 2 + .../src/main/resources/templates-3/form.html | 20 ++++++++ .../src/main/resources/templates-3/index.html | 29 +++++++++++ .../main/resources/templates-3/result.html | 19 +++++++ 6 files changed, 167 insertions(+) create mode 100644 spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/expression/Dino.java create mode 100644 spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/expression/DinoController.java create mode 100644 spring-web-modules/spring-thymeleaf-5/src/main/resources/messages.properties create mode 100644 spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/form.html create mode 100644 spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/index.html create mode 100644 spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/result.html diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/expression/Dino.java b/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/expression/Dino.java new file mode 100644 index 0000000000..c1c0a2dfae --- /dev/null +++ b/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/expression/Dino.java @@ -0,0 +1,51 @@ +package com.baeldung.thymeleaf.expression; + +public class Dino { + private int id; + private String name; + private String color; + private String weight; + + public Dino(int id, String name, String color, String weight) { + this.id = id; + this.name = name; + this.color = color; + this.weight = weight; + } + + public Dino() { + + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String getWeight() { + return weight; + } + + public void setWeight(String weight) { + this.weight = weight; + } +} diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/expression/DinoController.java b/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/expression/DinoController.java new file mode 100644 index 0000000000..6cbf1bae0e --- /dev/null +++ b/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/expression/DinoController.java @@ -0,0 +1,46 @@ +package com.baeldung.thymeleaf.expression; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +import java.util.ArrayList; +import java.util.List; + +@Controller +public class DinoController { + + ArrayList dinos = new ArrayList(); + + @RequestMapping("/") + public String dinoList(Model model) { + Dino dinos = new Dino(1, "alpha", "red", "50kg"); + + model.addAttribute("dinos", new Dino()); + model.addAttribute("dinos", dinos); + System.out.println(dinos); + + return "templates-3/index"; + + } + + @RequestMapping("/create") + public String dinoCreate(Model model) { + + model.addAttribute("dinos", new Dino()); + + return "templates-3/form"; + + } + + @PostMapping("/dino") + public String dinoSubmit(@ModelAttribute Dino dino, Model model) { + + model.addAttribute("dino", dino); + return "templates-3/result"; + } + +} diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/resources/messages.properties b/spring-web-modules/spring-thymeleaf-5/src/main/resources/messages.properties new file mode 100644 index 0000000000..8a0132d771 --- /dev/null +++ b/spring-web-modules/spring-thymeleaf-5/src/main/resources/messages.properties @@ -0,0 +1,2 @@ +welcome.message=welcome to Dino world. +dino.color=red is my favourite, mine is {0} \ No newline at end of file diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/form.html b/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/form.html new file mode 100644 index 0000000000..528f5405d1 --- /dev/null +++ b/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/form.html @@ -0,0 +1,20 @@ + + + + Dino World + + + +

+

Form

+
+

Id:

+

Name:

+

Color:

+

Weight:

+

+
+ + +
+ diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/index.html b/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/index.html new file mode 100644 index 0000000000..4928b50df0 --- /dev/null +++ b/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/index.html @@ -0,0 +1,29 @@ + + + + Dino World + + + +

+

+ +
+ [[${dinos.id}]]/ + [[${dinos.name}]]/ + [[${dinos.weight}]]/ + [[${dinos.color}]]/ +
+
+

+

+

+

+

+ Submit Another Dino + +
+

Copyright 2022

+
+
+ diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/result.html b/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/result.html new file mode 100644 index 0000000000..70edecbc35 --- /dev/null +++ b/spring-web-modules/spring-thymeleaf-5/src/main/resources/templates-3/result.html @@ -0,0 +1,19 @@ + + + + Dino World + + + +

+

+

Result

+

+

+ Submit Another Dino + Baeldung Home + + + +

+