This commit is contained in:
Amy DeGregorio 2019-06-25 08:57:18 -04:00 committed by maibin
parent 3e4607e724
commit 09f62ffe68
7 changed files with 172 additions and 0 deletions

View File

@ -0,0 +1,40 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-thymeleaf-2</artifactId>
<name>spring-thymeleaf-2</name>
<packaging>war</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
<finalName>spring-thymeleaf-2</finalName>
</build>
</project>

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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 + "]";
}
}

View File

@ -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";
}
}

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Enums in Thymeleaf</title>
</head>
<body>
<form th:action="@{/widget/add}" method="post" th:object="${widget}">
<h3>Add New Widget</h3>
<label for="name">Name: </label>
<input type="text" name="name"/>
<label for="color">Color: </label>
<select name="color">
<option th:each="colorOpt : ${T(com.baeldung.thymeleaf.enums.Color).values()}" th:value="${colorOpt}" th:text="${colorOpt.displayValue}"></option>
</select>
<input type="submit" value="Submit" />
</form>
</body>
</html>

View File

@ -0,0 +1,30 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Enums in Thymeleaf</title>
</head>
<body>
<h3>View Widget</h3>
<div>
<label for="name">Name: </label>
<span th:text="${widget.name}"></span>
</div>
<div>
<label for="color">Color: </label>
<span th:text="${widget.color.displayValue}"></span>
</div>
<div th:if="${widget.color == T(com.baeldung.thymeleaf.enums.Color).RED}">
This color screams danger.
</div>
<div th:if="${widget.color.name() == 'GREEN'}">
Green is for go.
</div>
<div th:switch="${widget.color}">
<span th:case="${T(com.baeldung.thymeleaf.enums.Color).RED}" style="color: red;">Alert</span>
<span th:case="${T(com.baeldung.thymeleaf.enums.Color).ORANGE}" style="color: orange;">Warning</span>
<span th:case="${T(com.baeldung.thymeleaf.enums.Color).YELLOW}" style="color: yellow;">Caution</span>
<span th:case="${T(com.baeldung.thymeleaf.enums.Color).GREEN}" style="color: green;">All Good</span>
</div>
</body>
</html>