Skip to content

Commit dba41e1

Browse files
fanatixanmaibin
authored andcommitted
working with booleans in thymeleaf (eugenp#4061)
1 parent 57a4f16 commit dba41e1

3 files changed

Lines changed: 230 additions & 4 deletions

File tree

‎spring-thymeleaf/README.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Spring MVC + Thymeleaf 3.0: New Features](http://www.baeldung.com/spring-thymeleaf-3)
1111
- [How to Work with Dates in Thymeleaef](http://www.baeldung.com/dates-in-thymeleaf)
1212
- [How to Create an Executable JAR with Maven](http://www.baeldung.com/executable-jar-with-maven)
13+
- [Working with Booleans in Thymeleaf](http://www.baeldung.com/working-with-booleans-in-thymeleaf)
1314

1415
### Build the Project
1516

@@ -22,11 +23,12 @@ mvn cargo:run
2223

2324
Access the pages using the URLs:
2425

25-
http://localhost:8082/spring-thymeleaf/
26-
http://localhost:8082/spring-thymeleaf/addStudent/
27-
http://localhost:8082/spring-thymeleaf/listStudents/
26+
- http://localhost:8082/spring-thymeleaf/
27+
- http://localhost:8082/spring-thymeleaf/addStudent/
28+
- http://localhost:8082/spring-thymeleaf/listStudents/
29+
- http://localhost:8082/spring-thymeleaf/booleans/
2830

29-
The first URL is the home page of the application. The home page has links to the other two pages.
31+
The first URL is the home page of the application. The home page has links to the second and third pages.
3032

3133
### Security
3234
The user/password required is: user1/user1Pass
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.thymeleaf.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
8+
/**
9+
* Controller to test boolean expressions
10+
*
11+
*/
12+
@Controller
13+
public class BooleanExpressionsController {
14+
15+
@RequestMapping(value = "/booleans", method = RequestMethod.GET)
16+
public String getDates(Model model) {
17+
// "truthy" values
18+
model.addAttribute("trueValue", true);
19+
model.addAttribute("one", 1);
20+
model.addAttribute("nonZeroCharacter", 'a');
21+
model.addAttribute("emptyString", "");
22+
model.addAttribute("foo", "foo");
23+
model.addAttribute("object", new Object());
24+
model.addAttribute("arrayOfZeros", new Integer[] { 0, 0 });
25+
model.addAttribute("arrayOfZeroAndOne", new Integer[] { 0, 1 });
26+
model.addAttribute("arrayOfOnes", new Integer[] { 1, 1 });
27+
28+
// "falsy" values
29+
model.addAttribute("nullValue", null);
30+
model.addAttribute("falseValue", false);
31+
model.addAttribute("zero", 0);
32+
model.addAttribute("zeroCharacter", '\0');
33+
model.addAttribute("falseString", "false");
34+
model.addAttribute("no", "no");
35+
model.addAttribute("off", "off");
36+
37+
model.addAttribute("isRaining", true);
38+
model.addAttribute("isSunny", true);
39+
model.addAttribute("isCold", false);
40+
model.addAttribute("isWarm", true);
41+
42+
return "booleans.html";
43+
}
44+
45+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml"
3+
xmlns:th="http://www.thymeleaf.org">
4+
<head>
5+
<meta charset="UTF-8">
6+
<title>Expression utility objects</title>
7+
<style>
8+
table {
9+
border-collapse: collapse;
10+
}
11+
12+
td {
13+
border: 1px solid black;
14+
padding: .5em;
15+
}
16+
</style>
17+
</head>
18+
<body>
19+
<h1>'Truthy' and 'falsy' expressions</h1>
20+
<ul>
21+
<li>'true' is evaluated to <strong th:text="${#bools.isTrue(trueValue)}"></strong></li>
22+
<li>'1' is evaluated to <strong th:text="${#bools.isTrue(one)}"></strong></li>
23+
<li>non zero character is evaluated to <strong th:text="${#bools.isTrue(nonZeroCharacter)}"></strong></li>
24+
<li>empty string is evaluated to <strong th:text="${#bools.isTrue(emptyString)}"></strong></li>
25+
<li>the string "foo" is evaluated to <strong th:text="${#bools.isTrue(foo)}"></strong></li>
26+
<li>an object is evaluated to <strong th:text="${#bools.isTrue(object)}"></strong></li>
27+
<li>the array [0, 0] is evaluated to <strong th:text="${#bools.isTrue(arrayOfZeros)}"></strong></li>
28+
<li>the array [0, 1] is evaluated to <strong th:text="${#bools.isTrue(arrayOfZeroAndOne)}"></strong></li>
29+
<li>the array [1, 1] is evaluated to <strong th:text="${#bools.isTrue(arrayOfOnes)}"></strong></li>
30+
31+
<li>null value is evaluated to <strong th:text="${#bools.isTrue(nullValue)}"></strong></li>
32+
<li>'false' is evaluated to <strong th:text="${#bools.isTrue(falseValue)}"></strong></li>
33+
<li>'0' is evaluated to <strong th:text="${#bools.isTrue(zero)}"></strong></li>
34+
<li>zero character is evaluated to <strong th:text="${#bools.isTrue(zeroCharacter)}"></strong></li>
35+
<li>the string "false" is evaluated to <strong th:text="${#bools.isTrue(falseString)}"></strong></li>
36+
<li>the string "no" is evaluated to <strong th:text="${#bools.isTrue(no)}"></strong></li>
37+
<li>the string "off" is evaluated to <strong th:text="${#bools.isTrue(off)}"></strong></li>
38+
</ul>
39+
40+
<h1>Using booleans as rendering conditions</h1>
41+
<table>
42+
<tr>
43+
<td></td>
44+
<td>th:if</td>
45+
<td>th:unless</td>
46+
</tr>
47+
<tr>
48+
<td>true</td>
49+
<td><span th:if="${true}">will be rendered</span></td>
50+
<td><span th:unless="${true}">won't be rendered</span></td>
51+
</tr>
52+
<tr>
53+
<td>false</td>
54+
<td><span th:if="${false}">won't be rendered</span></td>
55+
<td><span th:unless="${false}">will be rendered</span></td>
56+
</tr>
57+
</table>
58+
59+
<h1>Boolean and conditional operators</h1>
60+
<table>
61+
<tr>
62+
<td>A</td>
63+
<td>B</td>
64+
<td>${A or B}</td>
65+
<td>${A} or ${B}</td>
66+
<td>${A and B}</td>
67+
<td>${A} and ${B}</td>
68+
<td>${!A}</td>
69+
<td>!${A}</td>
70+
<td>${not A}</td>
71+
<td>not ${A}</td>
72+
</tr>
73+
<tr>
74+
<td>true</td>
75+
<td>true</td>
76+
<td th:text="${true or true}"></td>
77+
<td th:text="${true} or ${true}"></td>
78+
<td th:text="${true and true}"></td>
79+
<td th:text="${true} and ${true}"></td>
80+
<td th:text="${!true}"></td>
81+
<td th:text="!${true}"></td>
82+
<td th:text="${not true}"></td>
83+
<td th:text="not ${true}"></td>
84+
</tr>
85+
<tr>
86+
<td>true</td>
87+
<td>false</td>
88+
<td th:text="${true or false}"></td>
89+
<td th:text="${true} or ${false}"></td>
90+
<td th:text="${true and false}"></td>
91+
<td th:text="${true} and ${false}"></td>
92+
<td th:text="${!true}"></td>
93+
<td th:text="!${true}"></td>
94+
<td th:text="${not true}"></td>
95+
<td th:text="not ${true}"></td>
96+
</tr>
97+
<tr>
98+
<td>false</td>
99+
<td>true</td>
100+
<td th:text="${false or true}"></td>
101+
<td th:text="${false} or ${true}"></td>
102+
<td th:text="${false and true}"></td>
103+
<td th:text="${false} and ${true}"></td>
104+
<td th:text="${!false}"></td>
105+
<td th:text="!${false}"></td>
106+
<td th:text="${not false}"></td>
107+
<td th:text="not ${false}"></td>
108+
</tr>
109+
<tr>
110+
<td>false</td>
111+
<td>false</td>
112+
<td th:text="${false or false}"></td>
113+
<td th:text="${false} or ${false}"></td>
114+
<td th:text="${false and false}"></td>
115+
<td th:text="${false} and ${false}"></td>
116+
<td th:text="${!false}"></td>
117+
<td th:text="!${false}"></td>
118+
<td th:text="${not false}"></td>
119+
<td th:text="not ${false}"></td>
120+
</tr>
121+
</table>
122+
123+
<ul>
124+
<li> the result of "true ? 'then'" is <strong th:text="true ? 'then'"></strong></li>
125+
<li> the result of "false ? 'then'" is <strong th:text="false ? 'then'"></strong></li>
126+
<li> the result of "true ? 'then' : 'else'" is <strong th:text="true ? 'then' : 'else'"></strong></li>
127+
<li> the result of "false ? 'then' : 'else'" is <strong th:text="false ? 'then' : 'else'"></strong></li>
128+
<li> the result of "'foo' ?: 'bar'" is <strong th:text="'foo' ?: 'bar'"></strong></li>
129+
<li> the result of "null ?: 'bar'" is <strong th:text="null ?: 'bar'"></strong></li>
130+
<li> the result of "0 ?: 'bar'" is <strong th:text="0 ?: 'bar'"></strong></li>
131+
<li> the result of "1 ?: 'bar'" is <strong th:text="1 ?: 'bar'"></strong></li>
132+
</ul>
133+
134+
<ul>
135+
<li><span th:if="${isRaining or isCold}">The weather is bad</span></li>
136+
<li><span th:if="${isRaining} or ${isCold}">The weather is bad</span></li>
137+
<li><span th:if="${isSunny and isWarm}">The weather is good</span></li>
138+
<li><span th:if="${isSunny} and ${isWarm}">The weather is good</span></li>
139+
<li><span th:if="${not isCold}">It's warm</span></li>
140+
<li><span th:if="${!isCold}">It's warm</span></li>
141+
<li><span th:if="not ${isCold}">It's warm</span></li>
142+
<li><span th:if="!${isCold}">It's warm</span></li>
143+
<li>It's <span th:text="${isCold} ? 'cold' : 'warm'"></span></li>
144+
<li><span th:text="${isRaining or isCold} ? 'The weather is bad'"></span></li>
145+
</ul>
146+
147+
<h1>#bools utility object</h1>
148+
<table>
149+
<tr>
150+
<td>Array</td>
151+
<td>#bools.arrayIsTrue()</td>
152+
<td>#bools.arrayIsFalse()</td>
153+
<td>#bools.arrayAnd()</td>
154+
<td>#bools.arrayOr()</td>
155+
</tr>
156+
<tr>
157+
<td>[0, 0]</td>
158+
<td th:text="${#strings.arrayJoin(#bools.arrayIsTrue(arrayOfZeros), ',')}"></td>
159+
<td th:text="${#strings.arrayJoin(#bools.arrayIsFalse(arrayOfZeros), ',')}"></td>
160+
<td th:text="${#bools.arrayAnd(arrayOfZeros)}"></td>
161+
<td th:text="${#bools.arrayOr(arrayOfZeros)}"></td>
162+
</tr>
163+
<tr>
164+
<td>[0, 1]</td>
165+
<td th:text="${#strings.arrayJoin(#bools.arrayIsTrue(arrayOfZeroAndOne), ',')}"></td>
166+
<td th:text="${#strings.arrayJoin(#bools.arrayIsFalse(arrayOfZeroAndOne), ',')}"></td>
167+
<td th:text="${#bools.arrayAnd(arrayOfZeroAndOne)}"></td>
168+
<td th:text="${#bools.arrayOr(arrayOfZeroAndOne)}"></td>
169+
</tr>
170+
<tr>
171+
<td>[1, 1]</td>
172+
<td th:text="${#strings.arrayJoin(#bools.arrayIsTrue(arrayOfOnes), ',')}"></td>
173+
<td th:text="${#strings.arrayJoin(#bools.arrayIsFalse(arrayOfOnes), ',')}"></td>
174+
<td th:text="${#bools.arrayAnd(arrayOfOnes)}"></td>
175+
<td th:text="${#bools.arrayOr(arrayOfOnes)}"></td>
176+
</tr>
177+
</table>
178+
</body>
179+
</html>

0 commit comments

Comments
 (0)