simple-thymeleaf-expression (#12981)
* simple-thymeleaf-expression * expression-update
This commit is contained in:
parent
74d6c30d3c
commit
06498b9d70
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<Dino> dinos = new ArrayList<Dino>();
|
||||
|
||||
@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";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
welcome.message=welcome to Dino world.
|
||||
dino.color=red is my favourite, mine is {0}
|
|
@ -0,0 +1,20 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="https://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Dino World</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
</head>
|
||||
<body>
|
||||
<h2 th:text="#{welcome.message}"></h2>
|
||||
<h1>Form</h1>
|
||||
<form action="#" th:action="@{/dino}" th:object="${dinos}" method="post">
|
||||
<p>Id: <input type="text" th:field="*{id}" /></p>
|
||||
<p>Name: <input type="text" th:field="*{name}" /></p>
|
||||
<p>Color: <input type="text" th:field="*{color}" /></p>
|
||||
<p>Weight: <input type="text" th:field="*{weight}" /></p>
|
||||
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
<div th:replace="~{index :: footer}"></div>
|
||||
</html>
|
|
@ -0,0 +1,29 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="https://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Dino World</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
</head>
|
||||
<body>
|
||||
<h2 th:text="#{welcome.message}"></h2>
|
||||
<h2 th:text="#{dino.color('blue')}"></h2>
|
||||
|
||||
<div>
|
||||
<span>[[${dinos.id}]]/<span>
|
||||
<span>[[${dinos.name}]]/<span>
|
||||
<span>[[${dinos.weight}]]/<span>
|
||||
<span>[[${dinos.color}]]/<span>
|
||||
</div>
|
||||
<div th:object="${dinos}">
|
||||
<p th:text="*{id}">
|
||||
<p th:text="*{name}">
|
||||
<p th:text="*{weight}">
|
||||
<p th:text="*{color}">
|
||||
</div>
|
||||
<a href="/create">Submit Another Dino</a>
|
||||
</body>
|
||||
<div th:fragment="footer">
|
||||
<p> Copyright 2022</p>
|
||||
<hr />
|
||||
</div>
|
||||
</html>
|
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html xmlns:th="https://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>Dino World</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
</head>
|
||||
<body>
|
||||
<h2 th:text="#{welcome.message}"></h2>
|
||||
<h2 th:text="#{dino.color(${dino.color})}"></h2>
|
||||
<h1>Result</h1>
|
||||
<p th:text="'id: ' + ${dino.id}" />
|
||||
<p th:text="'content: ' + ${dino.name}" />
|
||||
<a th:href="@{/create}">Submit Another Dino</a>
|
||||
<a th:href="@{http://www.baeldung.com}"> Baeldung Home</a>
|
||||
</body>
|
||||
|
||||
|
||||
<div th:replace="~{index :: footer}"></div>
|
||||
</html>
|
Loading…
Reference in New Issue