From 2c75afbbe3b03bc8400fc65427b319a9d3698847 Mon Sep 17 00:00:00 2001 From: Constantin Date: Mon, 29 Jan 2024 15:12:21 +0200 Subject: [PATCH 1/6] BAEL-7263: Check if a Thymeleaf variable is defined --- .../thymeleaf/controller/HomeController.java | 13 ++- .../WEB-INF/views/checkVariableIsDefined.html | 14 +++ .../HomeControllerIntegrationTest.java | 92 +++++++++++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html create mode 100644 spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java b/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java index 8bbcd8fdb7..fb47cd574f 100644 --- a/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java +++ b/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java @@ -11,7 +11,7 @@ import java.util.Locale; /** * Handles requests for the application home page. - * + * */ @Controller public class HomeController { @@ -23,4 +23,15 @@ public class HomeController { return "home.html"; } + @RequestMapping(value = "/variable-defined", method = RequestMethod.GET) + public String getDefinedVariables(Model model) { + DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.getDefault()); + model.addAttribute("serverTime", dateFormat.format(new Date())); + return "checkVariableIsDefined.html"; + } + + @RequestMapping(value = "/variable-not-defined", method = RequestMethod.GET) + public String getNotDefinedVariables(Model model) { + return "checkVariableIsDefined.html"; + } } diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html b/spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html new file mode 100644 index 0000000000..56dde6160d --- /dev/null +++ b/spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html @@ -0,0 +1,14 @@ + + + + How to Check if a Variable is Defined in Thymeleaf + + + +
+
+
+ + + diff --git a/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java b/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java new file mode 100644 index 0000000000..33bf74a3ea --- /dev/null +++ b/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java @@ -0,0 +1,92 @@ +package com.baeldung.thymeleaf.controller; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.not; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import com.baeldung.thymeleaf.config.WebApp; +import com.baeldung.thymeleaf.config.WebMVCConfig; + +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class }) +public class HomeControllerIntegrationTest { + private static final String CTX_OBJECT_MSG = "Server Time Using the #ctx Object Is: "; + private static final String IF_CONDITIONAL_MSG = "Server Time Using #th:if Conditional Is: "; + private static final String UNLESS_CONDITIONAL_MSG = "Server Time Using #th:unless Conditional Is: "; + + @Autowired + private WebApplicationContext wac; + + private MockMvc mockMvc; + + @Before + public void setup() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac) + .build(); + } + + @Test + public void whenVariableIsDefined_thenCtxObjectContainsVariable() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/variable-defined")) + .andExpect(status().isOk()) + .andExpect(view().name("checkVariableIsDefined.html")) + .andExpect(content().string(containsString(CTX_OBJECT_MSG))); + } + + @Test + public void whenVariableNotDefined_thenCtxObjectDoesNotContainVariable() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/variable-not-defined")) + .andExpect(status().isOk()) + .andExpect(view().name("checkVariableIsDefined.html")) + .andExpect(content().string(not(containsString(CTX_OBJECT_MSG)))); + } + + @Test + public void whenVariableIsDefined_thenIfConditionalIsTrue() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/variable-defined")) + .andExpect(status().isOk()) + .andExpect(view().name("checkVariableIsDefined.html")) + .andExpect(content().string(containsString(IF_CONDITIONAL_MSG))); + } + + @Test + public void whenVariableIsNotDefined_thenIfConditionalIsFalse() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/variable-not-defined")) + .andExpect(status().isOk()) + .andExpect(view().name("checkVariableIsDefined.html")) + .andExpect(content().string(not(containsString(IF_CONDITIONAL_MSG)))); + } + + @Test + public void whenVariableIsDefined_thenUnlessConditionalIsTrue() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/variable-defined")) + .andExpect(status().isOk()) + .andExpect(view().name("checkVariableIsDefined.html")) + .andExpect(content().string(containsString(IF_CONDITIONAL_MSG))); + } + + @Test + public void whenUsingUnlessConditionalAndVariableIsNotDefined_thenVariableIsNotPrinted() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/variable-not-defined")) + .andExpect(status().isOk()) + .andExpect(view().name("checkVariableIsDefined.html")) + .andExpect(content().string(not(containsString(UNLESS_CONDITIONAL_MSG)))); + } +} \ No newline at end of file From 550788a96d55f20dd2e7f8b10c9d2503d9eb245d Mon Sep 17 00:00:00 2001 From: Constantin Date: Mon, 29 Jan 2024 15:45:07 +0200 Subject: [PATCH 2/6] BAEL-7263: Update method names --- .../com/baeldung/thymeleaf/controller/HomeController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java b/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java index fb47cd574f..091662fb33 100644 --- a/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java +++ b/spring-web-modules/spring-thymeleaf-5/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java @@ -24,14 +24,14 @@ public class HomeController { } @RequestMapping(value = "/variable-defined", method = RequestMethod.GET) - public String getDefinedVariables(Model model) { + public String getVariableIsDefined(Model model) { DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.getDefault()); model.addAttribute("serverTime", dateFormat.format(new Date())); return "checkVariableIsDefined.html"; } @RequestMapping(value = "/variable-not-defined", method = RequestMethod.GET) - public String getNotDefinedVariables(Model model) { + public String getVariableIsNotDefined(Model model) { return "checkVariableIsDefined.html"; } } From 4f0a16e4114ab61431ffcfd68e13e1cf8b069788 Mon Sep 17 00:00:00 2001 From: Constantin Date: Mon, 29 Jan 2024 15:50:28 +0200 Subject: [PATCH 3/6] BAEL-7263: Check if variable is not null --- .../src/main/webapp/WEB-INF/views/checkVariableIsDefined.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html b/spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html index 56dde6160d..47992899f8 100644 --- a/spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html +++ b/spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html @@ -7,7 +7,7 @@
-
+
From ebdae78e5eb1c4db6fbe48b669994e409369bdb5 Mon Sep 17 00:00:00 2001 From: Constantin Date: Tue, 30 Jan 2024 15:21:19 +0200 Subject: [PATCH 4/6] BAEL-7263: Update test name --- .../thymeleaf/controller/HomeControllerIntegrationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java b/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java index 33bf74a3ea..6d9d91c689 100644 --- a/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java +++ b/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java @@ -83,7 +83,7 @@ public class HomeControllerIntegrationTest { } @Test - public void whenUsingUnlessConditionalAndVariableIsNotDefined_thenVariableIsNotPrinted() throws Exception { + public void whenVariableIsDefined_thenUnlessConditionalIsFalse() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/variable-not-defined")) .andExpect(status().isOk()) .andExpect(view().name("checkVariableIsDefined.html")) From 003e89f1c6cb888d33c87911e35bf91d95e7e6cb Mon Sep 17 00:00:00 2001 From: Constantin Date: Tue, 30 Jan 2024 15:22:14 +0200 Subject: [PATCH 5/6] BAEL-7263: Update test name --- .../thymeleaf/controller/HomeControllerIntegrationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java b/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java index 6d9d91c689..d4e3c4aa28 100644 --- a/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java +++ b/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java @@ -83,7 +83,7 @@ public class HomeControllerIntegrationTest { } @Test - public void whenVariableIsDefined_thenUnlessConditionalIsFalse() throws Exception { + public void whenVariableIsNotDefined_thenUnlessConditionalIsFalse() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/variable-not-defined")) .andExpect(status().isOk()) .andExpect(view().name("checkVariableIsDefined.html")) From 846d76351506fa7251366d1a9e9718e2395fd5b6 Mon Sep 17 00:00:00 2001 From: Constantin Date: Thu, 1 Feb 2024 20:06:26 +0200 Subject: [PATCH 6/6] BAEL-7263: Highlight scenarios where defined variable is evaluated as false --- .../webapp/WEB-INF/views/checkVariableIsDefined.html | 5 +++++ .../controller/HomeControllerIntegrationTest.java | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html b/spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html index 47992899f8..7d995718f6 100644 --- a/spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html +++ b/spring-web-modules/spring-thymeleaf-5/src/main/webapp/WEB-INF/views/checkVariableIsDefined.html @@ -10,5 +10,10 @@
+
+
+
+
+ diff --git a/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java b/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java index d4e3c4aa28..9ebc80c658 100644 --- a/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java +++ b/spring-web-modules/spring-thymeleaf-5/src/test/java/com/baeldung/thymeleaf/controller/HomeControllerIntegrationTest.java @@ -74,6 +74,17 @@ public class HomeControllerIntegrationTest { .andExpect(content().string(not(containsString(IF_CONDITIONAL_MSG)))); } + @Test + public void whenVariableIsDefinedAndNotTrue_thenIfConditionalIsFalse() throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get("/variable-defined")) + .andExpect(status().isOk()) + .andExpect(view().name("checkVariableIsDefined.html")) + .andExpect(content().string(not(containsString("Evaluating \"false\"")))) + .andExpect(content().string(not(containsString("Evaluating \"no\"")))) + .andExpect(content().string(not(containsString("Evaluating \"off\"")))) + .andExpect(content().string(not(containsString("Evaluating 0")))); + } + @Test public void whenVariableIsDefined_thenUnlessConditionalIsTrue() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/variable-defined"))