Merge branch 'BAEL-3283-latest' into BAEL-3283

This commit is contained in:
sandip singh
2019-10-31 23:25:14 +05:30
parent db85c8f275
commit f6c3f9ebcb
20562 changed files with 1643286 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
## Spring Thymeleaf
This module contains articles about Spring with Thymeleaf
## Relevant Articles:
- [Working with Enums in Thymeleaf](https://www.baeldung.com/thymeleaf-enums)
- [Changing the Thymeleaf Template Directory in Spring Boot](https://www.baeldung.com/spring-thymeleaf-template-directory)
- [Spring Request Parameters with Thymeleaf](https://www.baeldung.com/spring-thymeleaf-request-parameters)
- [Thymeleaf lists Utility Object](https://www.baeldung.com/thymeleaf-lists-utility)
- [Working with Arrays in Thymeleaf](https://www.baeldung.com/thymeleaf-arrays)
- [Spring Path Variables with Thymeleaf](https://www.baeldung.com/spring-thymeleaf-path-variables)
- [Working with Boolean in Thymeleaf](https://www.baeldung.com/thymeleaf-boolean)
- [Working With Custom HTML Attributes in Thymeleaf](https://www.baeldung.com/thymeleaf-custom-html-attributes)
- [How to Create an Executable JAR with Maven](https://www.baeldung.com/executable-jar-with-maven)
- [[<-- prev]](/spring-thymeleaf)
+68
View File
@@ -0,0 +1,68 @@
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat7-maven-plugin.version}</version>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<path>/</path>
<enableNaming>false</enableNaming>
<finalName>webapp.jar</finalName>
<charset>utf-8</charset>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>spring-thymeleaf-2</finalName>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
</properties>
</project>
@@ -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);
}
}
@@ -0,0 +1,23 @@
package com.baeldung.thymeleaf;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
@Configuration
public class ThymeleafConfig {
@Bean
public ClassLoaderTemplateResolver secondaryTemplateResolver() {
ClassLoaderTemplateResolver secondaryTemplateResolver = new ClassLoaderTemplateResolver();
secondaryTemplateResolver.setPrefix("templates-2/");
secondaryTemplateResolver.setSuffix(".html");
secondaryTemplateResolver.setTemplateMode(TemplateMode.HTML);
secondaryTemplateResolver.setCharacterEncoding("UTF-8");
secondaryTemplateResolver.setOrder(1);
secondaryTemplateResolver.setCheckExistence(true);
return secondaryTemplateResolver;
}
}
@@ -0,0 +1,17 @@
package com.baeldung.thymeleaf.arrays;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ThymeleafArrayController {
@GetMapping("/arrays")
public String arrayController(Model model) {
String[] continents = {"Africa", "Antarctica", "Asia", "Australia", "Europe", "North America", "Sourth America"};
model.addAttribute("continents", continents);
return "continents";
}
}
@@ -0,0 +1,45 @@
package com.baeldung.thymeleaf.booleanexpressions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Controller to test boolean expressions
*
*/
@Controller
public class BooleanExpressionsController {
@RequestMapping(value = "/booleans", method = RequestMethod.GET)
public String getDates(Model model) {
// "truthy" values
model.addAttribute("trueValue", true);
model.addAttribute("one", 1);
model.addAttribute("nonZeroCharacter", 'a');
model.addAttribute("emptyString", "");
model.addAttribute("foo", "foo");
model.addAttribute("object", new Object());
model.addAttribute("arrayOfZeros", new Integer[] { 0, 0 });
model.addAttribute("arrayOfZeroAndOne", new Integer[] { 0, 1 });
model.addAttribute("arrayOfOnes", new Integer[] { 1, 1 });
// "falsy" values
model.addAttribute("nullValue", null);
model.addAttribute("falseValue", false);
model.addAttribute("zero", 0);
model.addAttribute("zeroCharacter", '\0');
model.addAttribute("falseString", "false");
model.addAttribute("no", "no");
model.addAttribute("off", "off");
model.addAttribute("isRaining", true);
model.addAttribute("isSunny", true);
model.addAttribute("isCold", false);
model.addAttribute("isWarm", true);
return "booleans.html";
}
}
@@ -0,0 +1,52 @@
package com.baeldung.thymeleaf.customhtml;
import java.util.Date;
public class Course {
private String name;
private String description;
private Date startDate;
private Date endDate;
private String teacher;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
}
@@ -0,0 +1,28 @@
package com.baeldung.thymeleaf.customhtml;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Handles requests for the student model.
*
*/
@Controller
public class CourseRegistrationController {
@RequestMapping(value = "/registerCourse", method = RequestMethod.POST)
public String register(@ModelAttribute Course course, Model model) {
model.addAttribute("successMessage", "You have successfully registered for course: " + course.getName() + ".");
return "templates/courseRegistration.html";
}
@RequestMapping(value = "/registerCourse", method = RequestMethod.GET)
public String register(Model model) {
model.addAttribute("course", new Course());
return "templates/courseRegistration.html";
}
}
@@ -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;
}
}
@@ -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 + "]";
}
}
@@ -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";
}
}
@@ -0,0 +1,64 @@
package com.baeldung.thymeleaf.lists;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/lists")
public class ListsController {
@GetMapping("/toList")
public String usingToList(Model model) {
List<String> colors = getColors();
String[] colorsArray = colors.toArray(new String[0]);
model.addAttribute("myArray", colorsArray);
return "lists/toList";
}
@GetMapping("/contains")
public String usingContains(Model model) {
model.addAttribute("myList", getColors());
model.addAttribute("others", getOtherColors());
return "lists/contains";
}
@GetMapping("/size")
public String usingSize(Model model) {
model.addAttribute("myList", getColors());
return "lists/size";
}
@GetMapping("/isEmpty")
public String usingIsEmpty(Model model) {
model.addAttribute("myList", getColors());
return "lists/isEmpty";
}
@GetMapping("/sort")
public String usingSort(Model model) {
model.addAttribute("myList", getColors());
model.addAttribute("reverse", Comparator.reverseOrder());
return "lists/sort";
}
private List<String> getColors() {
List<String> colors = new ArrayList<>();
colors.add("green");
colors.add("yellow");
colors.add("red");
colors.add("blue");
return colors;
}
private List<String> getOtherColors() {
List<String> colors = new ArrayList<>();
colors.add("green");
colors.add("blue");
return colors;
}
}
@@ -0,0 +1,28 @@
package com.baeldung.thymeleaf.pathvariables;
public class Detail {
private int id;
private String description;
public Detail(int id, String description) {
super();
this.id = id;
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
@@ -0,0 +1,39 @@
package com.baeldung.thymeleaf.pathvariables;
import java.util.List;
public class Item {
private int id;
private String name;
private List<Detail> details;
public Item(int id, String name) {
super();
this.id = id;
this.name = name;
}
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 List<Detail> getDetails() {
return details;
}
public void setDetails(List<Detail> details) {
this.details = details;
}
}
@@ -0,0 +1,62 @@
package com.baeldung.thymeleaf.pathvariables;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class PathVariablesController {
private List<Item> items = new ArrayList<Item>();
public PathVariablesController() {
Item item1 = new Item(1, "First Item");
List<Detail> item1Details = new ArrayList<>();
item1Details.add(new Detail(1, "Green"));
item1Details.add(new Detail(2, "Large"));
item1.setDetails(item1Details);
items.add(item1);
Item item2 = new Item(2, "Second Item");
List<Detail> item2Details = new ArrayList<>();
item2Details.add(new Detail(1, "Red"));
item2Details.add(new Detail(2, "Medium"));
item2.setDetails(item2Details);
items.add(item2);
}
@GetMapping("/pathvars")
public String start(Model model) {
model.addAttribute("items", items);
return "pathvariables/index";
}
@GetMapping("/pathvars/single/{id}")
public String singlePathVariable(@PathVariable("id") int id, Model model) {
if (id == 1) {
model.addAttribute("item", new Item(1, "First Item"));
} else {
model.addAttribute("item", new Item(2, "Second Item"));
}
return "pathvariables/view";
}
@GetMapping("/pathvars/item/{itemId}/detail/{detailId}")
public String multiplePathVariable(@PathVariable("itemId") int itemId, @PathVariable("detailId") int detailId, Model model) {
for (Item item : items) {
if (item.getId() == itemId) {
model.addAttribute("item", item);
for (Detail detail : item.getDetails()) {
if (detail.getId() == detailId) {
model.addAttribute("detail", detail);
}
}
}
}
return "pathvariables/view";
}
}
@@ -0,0 +1,28 @@
package com.baeldung.thymeleaf.requestparameters;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import static java.util.Arrays.asList;
@Controller
public class ParticipantController {
@RequestMapping("/participants")
public String index(
@RequestParam(value = "participant", required = false) String participant,
@RequestParam(value = "country", required = false) String country,
@RequestParam(value = "action", required = false) String action,
@RequestParam(value = "id", required = false) Integer id,
Model model
) {
model.addAttribute("id", id);
List<Integer> userIds = asList(1,2,3,4);
model.addAttribute("userIds", userIds);
return "participants";
}
}
@@ -0,0 +1,13 @@
package com.baeldung.thymeleaf.templatedir;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "hello";
}
}
@@ -0,0 +1 @@
#spring.thymeleaf.prefix=classpath:/templates-2/
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Enums in Thymeleaf</title>
</head>
<body>
<h2>Hello from 'templates/templates-2'</h2>
</body>
</html>
@@ -0,0 +1,32 @@
<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<h2>Enter participant</h2>
<form th:action="@{/participants}">
<input type="text" th:name="participant"/>
<select name="country">
<option value="de">Germany</option>
<option value="nl">Netherlands</option>
<option value="pl">Poland</option>
<option value="lv">Latvia</option>
</select>
<button type="submit" th:name="action" th:value="in">check-in</button>
<button type="submit" th:name="action" th:value="out">check-out</button>
</form>
<th:block th:if="${id != null}">
<h2 >User Details</h2>
<p>Details for user [[${id}]] ...</p>
</th:block>
<h2>Users</h2>
<th:block th:each="userId: ${userIds}">
<p>
<a th:href="@{/participants(id=${userId})}"> User [[${userId}]]</a>
</p>
</th:block>
</html>
@@ -0,0 +1,179 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Expression utility objects</title>
<style>
table {
border-collapse: collapse;
}
td {
border: 1px solid black;
padding: .5em;
}
</style>
</head>
<body>
<h1>'Truthy' and 'falsy' expressions</h1>
<ul>
<li>'true' is evaluated to <strong th:text="${#bools.isTrue(trueValue)}"></strong></li>
<li>'1' is evaluated to <strong th:text="${#bools.isTrue(one)}"></strong></li>
<li>non zero character is evaluated to <strong th:text="${#bools.isTrue(nonZeroCharacter)}"></strong></li>
<li>empty string is evaluated to <strong th:text="${#bools.isTrue(emptyString)}"></strong></li>
<li>the string "foo" is evaluated to <strong th:text="${#bools.isTrue(foo)}"></strong></li>
<li>an object is evaluated to <strong th:text="${#bools.isTrue(object)}"></strong></li>
<li>the array [0, 0] is evaluated to <strong th:text="${#bools.isTrue(arrayOfZeros)}"></strong></li>
<li>the array [0, 1] is evaluated to <strong th:text="${#bools.isTrue(arrayOfZeroAndOne)}"></strong></li>
<li>the array [1, 1] is evaluated to <strong th:text="${#bools.isTrue(arrayOfOnes)}"></strong></li>
<li>null value is evaluated to <strong th:text="${#bools.isTrue(nullValue)}"></strong></li>
<li>'false' is evaluated to <strong th:text="${#bools.isTrue(falseValue)}"></strong></li>
<li>'0' is evaluated to <strong th:text="${#bools.isTrue(zero)}"></strong></li>
<li>zero character is evaluated to <strong th:text="${#bools.isTrue(zeroCharacter)}"></strong></li>
<li>the string "false" is evaluated to <strong th:text="${#bools.isTrue(falseString)}"></strong></li>
<li>the string "no" is evaluated to <strong th:text="${#bools.isTrue(no)}"></strong></li>
<li>the string "off" is evaluated to <strong th:text="${#bools.isTrue(off)}"></strong></li>
</ul>
<h1>Using booleans as rendering conditions</h1>
<table>
<tr>
<td></td>
<td>th:if</td>
<td>th:unless</td>
</tr>
<tr>
<td>true</td>
<td><span th:if="${true}">will be rendered</span></td>
<td><span th:unless="${true}">won't be rendered</span></td>
</tr>
<tr>
<td>false</td>
<td><span th:if="${false}">won't be rendered</span></td>
<td><span th:unless="${false}">will be rendered</span></td>
</tr>
</table>
<h1>Boolean and conditional operators</h1>
<table>
<tr>
<td>A</td>
<td>B</td>
<td>${A or B}</td>
<td>${A} or ${B}</td>
<td>${A and B}</td>
<td>${A} and ${B}</td>
<td>${!A}</td>
<td>!${A}</td>
<td>${not A}</td>
<td>not ${A}</td>
</tr>
<tr>
<td>true</td>
<td>true</td>
<td th:text="${true or true}"></td>
<td th:text="${true} or ${true}"></td>
<td th:text="${true and true}"></td>
<td th:text="${true} and ${true}"></td>
<td th:text="${!true}"></td>
<td th:text="!${true}"></td>
<td th:text="${not true}"></td>
<td th:text="not ${true}"></td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td th:text="${true or false}"></td>
<td th:text="${true} or ${false}"></td>
<td th:text="${true and false}"></td>
<td th:text="${true} and ${false}"></td>
<td th:text="${!true}"></td>
<td th:text="!${true}"></td>
<td th:text="${not true}"></td>
<td th:text="not ${true}"></td>
</tr>
<tr>
<td>false</td>
<td>true</td>
<td th:text="${false or true}"></td>
<td th:text="${false} or ${true}"></td>
<td th:text="${false and true}"></td>
<td th:text="${false} and ${true}"></td>
<td th:text="${!false}"></td>
<td th:text="!${false}"></td>
<td th:text="${not false}"></td>
<td th:text="not ${false}"></td>
</tr>
<tr>
<td>false</td>
<td>false</td>
<td th:text="${false or false}"></td>
<td th:text="${false} or ${false}"></td>
<td th:text="${false and false}"></td>
<td th:text="${false} and ${false}"></td>
<td th:text="${!false}"></td>
<td th:text="!${false}"></td>
<td th:text="${not false}"></td>
<td th:text="not ${false}"></td>
</tr>
</table>
<ul>
<li> the result of "true ? 'then'" is <strong th:text="true ? 'then'"></strong></li>
<li> the result of "false ? 'then'" is <strong th:text="false ? 'then'"></strong></li>
<li> the result of "true ? 'then' : 'else'" is <strong th:text="true ? 'then' : 'else'"></strong></li>
<li> the result of "false ? 'then' : 'else'" is <strong th:text="false ? 'then' : 'else'"></strong></li>
<li> the result of "'foo' ?: 'bar'" is <strong th:text="'foo' ?: 'bar'"></strong></li>
<li> the result of "null ?: 'bar'" is <strong th:text="null ?: 'bar'"></strong></li>
<li> the result of "0 ?: 'bar'" is <strong th:text="0 ?: 'bar'"></strong></li>
<li> the result of "1 ?: 'bar'" is <strong th:text="1 ?: 'bar'"></strong></li>
</ul>
<ul>
<li><span th:if="${isRaining or isCold}">The weather is bad</span></li>
<li><span th:if="${isRaining} or ${isCold}">The weather is bad</span></li>
<li><span th:if="${isSunny and isWarm}">The weather is good</span></li>
<li><span th:if="${isSunny} and ${isWarm}">The weather is good</span></li>
<li><span th:if="${not isCold}">It's warm</span></li>
<li><span th:if="${!isCold}">It's warm</span></li>
<li><span th:if="not ${isCold}">It's warm</span></li>
<li><span th:if="!${isCold}">It's warm</span></li>
<li>It's <span th:text="${isCold} ? 'cold' : 'warm'"></span></li>
<li><span th:text="${isRaining or isCold} ? 'The weather is bad'"></span></li>
</ul>
<h1>#bools utility object</h1>
<table>
<tr>
<td>Array</td>
<td>#bools.arrayIsTrue()</td>
<td>#bools.arrayIsFalse()</td>
<td>#bools.arrayAnd()</td>
<td>#bools.arrayOr()</td>
</tr>
<tr>
<td>[0, 0]</td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsTrue(arrayOfZeros), ',')}"></td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsFalse(arrayOfZeros), ',')}"></td>
<td th:text="${#bools.arrayAnd(arrayOfZeros)}"></td>
<td th:text="${#bools.arrayOr(arrayOfZeros)}"></td>
</tr>
<tr>
<td>[0, 1]</td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsTrue(arrayOfZeroAndOne), ',')}"></td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsFalse(arrayOfZeroAndOne), ',')}"></td>
<td th:text="${#bools.arrayAnd(arrayOfZeroAndOne)}"></td>
<td th:text="${#bools.arrayOr(arrayOfZeroAndOne)}"></td>
</tr>
<tr>
<td>[1, 1]</td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsTrue(arrayOfOnes), ',')}"></td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsFalse(arrayOfOnes), ',')}"></td>
<td th:text="${#bools.arrayAnd(arrayOfOnes)}"></td>
<td th:text="${#bools.arrayOr(arrayOfOnes)}"></td>
</tr>
</table>
</body>
</html>
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Arrays in Thymeleaf</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h3>THE CONTINENTS</h3>
<p>
We have a total of <span th:text="${continents.length}"></span>
continents in this world. Following is a list showing their order
based on the number of population in each of them.
</p>
<ol>
<li th:text="${continents[2]}"></li>
<li th:text="${continents[0]}"></li>
<li th:text="${continents[4]}"></li>
<li th:text="${continents[5]}"></li>
<li th:text="${continents[6]}"></li>
<li th:text="${continents[3]}"></li>
<li th:text="${continents[1]}"></li>
</ol>
<ul th:each="continent : ${continents}">
<li th:text="${continent}"></li>
</ul>
<h4 th:each="continent : ${continents}" th:text="${continent}"></h4>
<p>
The greatest <span th:text="${#arrays.length(continents)}"></span>
continents.
</p>
<p>
Europe is a continent: <span
th:text="${#arrays.contains(continents, 'Europe')}"></span>.
</p>
<p>
Array of continents is empty <span
th:text="${#arrays.isEmpty(continents)}"></span>.
</p>
</body>
</html>
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Register for course</title>
<script>
function validateForm() {
var e = document.getElementById("course");
var value = e.options[e.selectedIndex].value;
if (value == '') {
var error = document.getElementById("errormesg");
error.textContent = e.getAttribute('data-validation-message');
return false;
}
return true;
}
</script>
</head>
<body>
<h3>Register Here</h3>
<form action="#" th:action="@{/registerCourse}" th:object="${course}"
method="post" onsubmit="return validateForm();">
<span id="errormesg" style="color: red"></span> <span
style="font-weight: bold" th:text="${successMessage}"></span>
<table>
<tr>
<td><label th:text="#{msg.courseName}+': '"></label></td>
<td><select id="course" th:field="*{name}"
th:data-validation-message="#{msg.courseName.mandatory}">
<option th:value="''" th:text="'Select'"></option>
<option th:value="'Mathematics'" th:text="'Mathematics'"></option>
<option th:value="'History'" th:text="'History'"></option>
</select></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
@@ -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>
@@ -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>
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Lists Utility Class in Thymeleaf</title>
</head>
<body>
myList contains red: <span th:text="${#lists.contains(myList, 'red')}"/>
myList contains red and green: <span th:text='${#lists.containsAll(myList, {"red", "green"})}'/>
</body>
</html>
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Lists Utility Class in Thymeleaf</title>
</head>
<body>
isEmpty Check : <span th:text="${#lists.isEmpty(myList)}"/>
<span th:unless="${#lists.isEmpty(myList)}">List is not empty</span>
</body>
</html>
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Lists Utility Class in Thymeleaf</title>
</head>
<body>
size: <span th:text="${#lists.size(myList)}"/>
</body>
</html>
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Lists Utility Class in Thymeleaf</title>
</head>
<body>
sort: <span th:text="${#lists.sort(myList)}"/>
sort with Comparator: <span th:text="${#lists.sort(myList, reverse)}"/>
</body>
</html>
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Lists Utility Class in Thymeleaf</title>
</head>
<body>
<span th:with="convertedList=${#lists.toList(myArray)}">
converted list size: <span th:text="${#lists.size(convertedList)}"/>
</span>
</body>
</html>
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>PathVariables in Thymeleaf</title>
</head>
<body>
<h3>Items</h3>
<div th:each="item : ${items}">
<a th:href="@{/pathvars/single/{id}(id = ${item.id})}">
<span th:text="${item.name}"></span>
</a>
<ul>
<li th:each="detail : ${item.details}">
<a th:href="@{/pathvars/item/{itemId}/detail/{detailId}(itemId = ${item.id}, detailId = ${detail.id})}">
<span th:text="${detail.description}"></span>
</a>
</li>
</ul>
</div>
</body>
</html>
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Pathvariables in Thymeleaf</title>
</head>
<body>
<h3>View Item</h3>
<div>
<label for="name">Name: </label>
<span th:text="${item.name}"></span>
</div>
<h4 th:if="${detail}">Detail</h4>
<div th:if="${detail}">
<label for="description">Description: </label>
<span th:text="${detail.description}"></span>
</div>
</body>
</html>
@@ -0,0 +1,60 @@
package com.baeldung.thymeleaf.lists;
import static org.hamcrest.CoreMatchers.containsString;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc(printOnlyOnFailure = false)
public class ListsControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
public void whenCalledToList_ThenConvertsToList() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/lists/toList"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("converted list size: <span>4</span>")));
}
@Test
public void whenCalledContains_ThenChecksMembership() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/lists/contains"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("myList contains red: <span>true</span>")))
.andExpect(content().string(containsString("myList contains red and green: <span>true</span>")));
}
@Test
public void whenCalledSize_ThenReturnsSize() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/lists/size"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("size: <span>4</span>")));
}
@Test
public void whenCalledSort_ThenSortsItems() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/lists/sort"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("sort: <span>[blue, green, red, yellow]</span>")))
.andExpect(content().string(containsString("sort with Comparator: <span>[yellow, red, green, blue]</span>")));
}
@Test
public void whenCalledIsEmpty_ThenChecksAnyMembers() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/lists/isEmpty"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("isEmpty Check : <span>false</span>")));
}
}