Merge branch 'master' into master

This commit is contained in:
Loredana Crusoveanu
2019-11-02 23:34:32 +02:00
committed by GitHub
141 changed files with 995 additions and 687 deletions
+9
View File
@@ -1,3 +1,7 @@
## Spring Thymeleaf
This module contains articles about Spring with Thymeleaf
## Relevant Articles:
- [Working with Enums in Thymeleaf](https://www.baeldung.com/thymeleaf-enums)
@@ -5,3 +9,8 @@
- [Spring Request Parameters with Thymeleaf](https://www.baeldung.com/spring-thymeleaf-request-parameters)
- [Thymeleaf lists Utility Object](https://www.baeldung.com/thymeleaf-lists-utility)
- [Spring Path Variables with Thymeleaf](https://www.baeldung.com/spring-thymeleaf-path-variables)
- [Working with Arrays in Thymeleaf](https://www.baeldung.com/thymeleaf-arrays)
- [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)
+22
View File
@@ -35,6 +35,27 @@
<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>
@@ -42,5 +63,6 @@
<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,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";
}
}
@@ -1,4 +1,4 @@
package com.baeldung.thymeleaf.controller;
package com.baeldung.thymeleaf.requestparameters;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@@ -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>