diff --git a/spring-thymeleaf/README.md b/spring-thymeleaf/README.md index 2c37e73ada..4d152aee8a 100644 --- a/spring-thymeleaf/README.md +++ b/spring-thymeleaf/README.md @@ -10,6 +10,7 @@ - [Spring MVC + Thymeleaf 3.0: New Features](http://www.baeldung.com/spring-thymeleaf-3) - [How to Work with Dates in Thymeleaef](http://www.baeldung.com/dates-in-thymeleaf) - [How to Create an Executable JAR with Maven](http://www.baeldung.com/executable-jar-with-maven) +- [Working with Booleans in Thymeleaf](http://www.baeldung.com/working-with-booleans-in-thymeleaf) ### Build the Project @@ -22,11 +23,12 @@ mvn cargo:run Access the pages using the URLs: -http://localhost:8082/spring-thymeleaf/ -http://localhost:8082/spring-thymeleaf/addStudent/ -http://localhost:8082/spring-thymeleaf/listStudents/ + - http://localhost:8082/spring-thymeleaf/ + - http://localhost:8082/spring-thymeleaf/addStudent/ + - http://localhost:8082/spring-thymeleaf/listStudents/ + - http://localhost:8082/spring-thymeleaf/booleans/ -The first URL is the home page of the application. The home page has links to the other two pages. +The first URL is the home page of the application. The home page has links to the second and third pages. ### Security The user/password required is: user1/user1Pass diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/BooleanExpressionsController.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/BooleanExpressionsController.java new file mode 100644 index 0000000000..3a640e1499 --- /dev/null +++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/BooleanExpressionsController.java @@ -0,0 +1,45 @@ +package com.baeldung.thymeleaf.controller; + +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"; + } + +} diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/booleans.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/booleans.html new file mode 100644 index 0000000000..63d894421b --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/booleans.html @@ -0,0 +1,179 @@ + + + + +Expression utility objects + + + +

'Truthy' and 'falsy' expressions

+ + +

Using booleans as rendering conditions

+ + + + + + + + + + + + + + + + +
th:ifth:unless
truewill be renderedwon't be rendered
falsewon't be renderedwill be rendered
+ +

Boolean and conditional operators

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
AB${A or B}${A} or ${B}${A and B}${A} and ${B}${!A}!${A}${not A}not ${A}
truetrue
truefalse
falsetrue
falsefalse
+ + + + + +

#bools utility object

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Array#bools.arrayIsTrue()#bools.arrayIsFalse()#bools.arrayAnd()#bools.arrayOr()
[0, 0]
[0, 1]
[1, 1]
+ + \ No newline at end of file