JAVA-3544: Move spring-thymeleaf-2 into spring-web-modules

This commit is contained in:
Krzysztof Woyke
2020-12-28 12:10:31 +01:00
parent d238a20f21
commit eb8724a0d7
44 changed files with 2 additions and 3 deletions
@@ -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,14 @@
package com.baeldung.thymeleaf.mvcdata;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baeldung.thymeleaf.mvcdata.repository.EmailData;
@Configuration
public class BeanConfig {
@Bean
public EmailData emailData() {
return new EmailData();
}
}
@@ -0,0 +1,63 @@
package com.baeldung.thymeleaf.mvcdata;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
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.RequestParam;
import com.baeldung.thymeleaf.mvcdata.repository.EmailData;
@Controller
public class EmailController {
private EmailData emailData = new EmailData();
private ServletContext servletContext;
public EmailController(ServletContext servletContext) {
this.servletContext = servletContext;
}
@GetMapping(value = "/email/modelattributes")
public String emailModel(Model model) {
model.addAttribute("emaildata", emailData);
return "mvcdata/email-model-attributes";
}
@ModelAttribute("emailModelAttribute")
EmailData emailModelAttribute() {
return emailData;
}
@GetMapping(value = "/email/requestparameters")
public String emailRequestParameters(
@RequestParam(value = "emailsubject") String emailSubject,
@RequestParam(value = "emailcontent") String emailContent,
@RequestParam(value = "emailaddress") String emailAddress1,
@RequestParam(value = "emailaddress") String emailAddress2,
@RequestParam(value = "emaillocale") String emailLocale) {
return "mvcdata/email-request-parameters";
}
@GetMapping("/email/sessionattributes")
public String emailSessionAttributes(HttpSession httpSession) {
httpSession.setAttribute("emaildata", emailData);
return "mvcdata/email-session-attributes";
}
@GetMapping("/email/servletcontext")
public String emailServletContext() {
servletContext.setAttribute("emailsubject", emailData.getEmailSubject());
servletContext.setAttribute("emailcontent", emailData.getEmailBody());
servletContext.setAttribute("emailaddress", emailData.getEmailAddress1());
servletContext.setAttribute("emaillocale", emailData.getEmailLocale());
return "mvcdata/email-servlet-context";
}
@GetMapping("/email/beandata")
public String emailBeanData() {
return "mvcdata/email-bean-data";
}
}
@@ -0,0 +1,48 @@
package com.baeldung.thymeleaf.mvcdata.repository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class EmailData implements Serializable {
private String emailSubject;
private String emailBody;
private String emailLocale;
private String emailAddress1;
private String emailAddress2;
public EmailData() {
this.emailSubject = "You have received a new message";
this.emailBody = "Good morning !";
this.emailLocale = "en-US";
this.emailAddress1 = "jhon.doe@example.com";
this.emailAddress2 = "mark.jakob@example.com";
}
public String getEmailSubject() {
return this.emailSubject;
}
public String getEmailBody() {
return this.emailBody;
}
public String getEmailLocale() {
return this.emailLocale;
}
public String getEmailAddress1() {
return this.emailAddress1;
}
public String getEmailAddress2() {
return this.emailAddress2;
}
public List<String> getEmailAddresses() {
List<String> emailAddresses = new ArrayList<>();
emailAddresses.add(getEmailAddress1());
emailAddresses.add(getEmailAddress2());
return emailAddresses;
}
}
@@ -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,14 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Subject</h1>
<p th:text="${@emailData.emailSubject}">Subject</p>
<h1>Content</h1>
<p th:text="${@emailData.emailBody}">Body</p>
<h1>Email address</h1>
<p th:text="${@emailData.emailAddress1}">Email address</p>
<h1>Language</h1>
<p th:text="${@emailData.emailLocale}">Language</p>
</body>
</html>
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Subject</h1>
<p th:text="${emaildata.emailSubject}">Subject</p>
<h1>Content</h1>
<p th:text="${emaildata.emailBody}"></p>
<h1>Email addresses</h1>
<p th:each="emailAddress : ${emailModelAttribute.getEmailAddresses()}">
<span th:text="${emailAddress}"></span>
</p>
<h1>Language</h1>
<p th:text="${emaildata.emailLocale}"></p>
</body>
</html>
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Subject</h1>
<p th:text="${param.emailsubject}">Subject</p>
<h1>Content</h1>
<p th:text="${param.emailcontent}"></p>
<h1>Email addresses</h1>
<p th:each="emailaddress : ${param.emailaddress}">
<span th:text="${emailaddress}"></span>
</p>
<h1>Email address 1</h1>
<p th:text="${param.emailaddress[0]}"></p>
<h1>Email address 2</h1>
<p th:text="${param.emailaddress[1]}"></p>
<h1>Language</h1>
<p th:text="${param.emaillocale}"></p>
</body>
</html>
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Subject</h1>
<p th:text="${#servletContext.getAttribute('emailsubject')}"></p>
<h1>Content</h1>
<p th:text="${#servletContext.getAttribute('emailcontent')}"></p>
<h1>Email address</h1>
<p th:text="${#servletContext.getAttribute('emailaddress')}"></p>
<h1>Language</h1>
<p th:text="${#servletContext.getAttribute('emaillocale')}"></p>
</body>
</html>
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Subject</h1>
<p th:text="${session.emaildata.emailSubject}"></p>
<h1>Content</h1>
<p th:text="${session.emaildata.emailBody}"></p>
<h1>Email address</h1>
<p th:text="${session.emaildata.emailAddress1}"></p>
<h1>Language</h1>
<p th:text="${#session.getAttribute('emaillocale')}"></p>
</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>")));
}
}
@@ -0,0 +1,72 @@
package com.baeldung.thymeleaf.mvcdata;
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;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import com.baeldung.thymeleaf.mvcdata.repository.EmailData;
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc(printOnlyOnFailure = false)
public class EmailControllerUnitTest {
EmailData emailData = new EmailData();
@Autowired
private MockMvc mockMvc;
@Test
public void whenCallModelAttributes_thenReturnEmailData() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/email/modelattributes"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("You have received a new message")));
}
@Test
public void whenCallRequestParameters_thenReturnEmailData() throws Exception {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("emailsubject", emailData.getEmailSubject());
params.add("emailcontent", emailData.getEmailBody());
params.add("emailaddress", emailData.getEmailAddress1());
params.add("emailaddress", emailData.getEmailAddress2());
params.add("emaillocale", emailData.getEmailLocale());
mockMvc.perform(MockMvcRequestBuilders.get("/email/requestparameters")
.params(params))
.andExpect(status().isOk())
.andExpect(content().string(containsString("en-US")));
}
@Test
public void whenCallSessionAttributes_thenReturnEmailData() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/email/sessionattributes"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("Good morning !")));
}
@Test
public void whenCallServletContext_thenReturnEmailData() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/email/servletcontext"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("jhon.doe@example.com")));
}
@Test
public void whenCallBeanData_thenReturnEmailData() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/email/beandata"))
.andExpect(status().isOk())
.andExpect(content().string(containsString("jhon.doe@example.com")));
}
}