From cd033c210911c6e2ecc9a2ddc8872cab5ce36f8d Mon Sep 17 00:00:00 2001 From: exaucae Date: Fri, 17 Jun 2022 15:14:02 +0000 Subject: [PATCH 1/4] fix: upgrade to cargo-maven3 Application couldn't start with previous setup. Plus, maven3 is the preferred way of using cargo now. See https://codehaus-cargo.github.io/cargo/Maven+3+Plugin.html Relevant section: Reg. Lifetime of our Maven 2 plugin " Please be aware that the Maven 2 / Maven 3 plugin of Codehaus Cargo has been retired with our version 1.9.0 and has been superseded by a Maven 3 only plugin. " --- spring-web-modules/spring-thymeleaf/pom.xml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/spring-web-modules/spring-thymeleaf/pom.xml b/spring-web-modules/spring-thymeleaf/pom.xml index 8201cb5c5b..94ae05ca11 100644 --- a/spring-web-modules/spring-thymeleaf/pom.xml +++ b/spring-web-modules/spring-thymeleaf/pom.xml @@ -115,10 +115,9 @@ org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} + cargo-maven3-plugin + ${cargo-maven3-plugin.version} - true jetty9x embedded @@ -143,7 +142,7 @@ 2.0.1.Final 6.0.11.Final - 1.6.1 + 1.9.9 \ No newline at end of file From 7adea75450f2d5c3834cd91f50036fc337e0573e Mon Sep 17 00:00:00 2001 From: exaucae Date: Fri, 17 Jun 2022 16:55:28 +0000 Subject: [PATCH 2/4] feat(wip): add function call examples --- .../controller/FunctionCallController.java | 16 ++++++ .../webapp/WEB-INF/views/functionCall.html | 28 +++++++++ .../FunctionCallIntegrationTest.java | 57 +++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FunctionCallController.java create mode 100644 spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html create mode 100644 spring-web-modules/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/FunctionCallIntegrationTest.java diff --git a/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FunctionCallController.java b/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FunctionCallController.java new file mode 100644 index 0000000000..b1869aa707 --- /dev/null +++ b/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FunctionCallController.java @@ -0,0 +1,16 @@ +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 +public class FunctionCallController { + + @RequestMapping(value = "/function-call", method = RequestMethod.GET) + public String getExampleHTML(Model model) { + model.addAttribute("num", 2); + return "functionCall.html"; + } +} diff --git a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html new file mode 100644 index 0000000000..38555c3d2b --- /dev/null +++ b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html @@ -0,0 +1,28 @@ + + + + +Thymeleaf: Javascript function call + + +
+
Thymeleaf: Javascript function call
+
+ +
+
+
Inline function call
+
+
Without a variable
+ +
+
+
With a variable
+ +
+
+
+ + + + \ No newline at end of file diff --git a/spring-web-modules/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/FunctionCallIntegrationTest.java b/spring-web-modules/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/FunctionCallIntegrationTest.java new file mode 100644 index 0000000000..90e8989aae --- /dev/null +++ b/spring-web-modules/spring-thymeleaf/src/test/java/com/baeldung/thymeleaf/controller/FunctionCallIntegrationTest.java @@ -0,0 +1,57 @@ +package com.baeldung.thymeleaf.controller; + +import com.baeldung.thymeleaf.config.InitSecurity; +import com.baeldung.thymeleaf.config.WebApp; +import com.baeldung.thymeleaf.config.WebMVCConfig; +import com.baeldung.thymeleaf.config.WebMVCSecurity; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.RequestPostProcessor; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import javax.servlet.Filter; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class }) +public class FunctionCallIntegrationTest { + + @Autowired + WebApplicationContext wac; + @Autowired + MockHttpSession session; + + private MockMvc mockMvc; + + @Autowired + private Filter springSecurityFilterChain; + + private RequestPostProcessor testUser() { + return user("user1").password("user1Pass").roles("USER"); + } + + @Before + public void setup() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build(); + } + + @Test + public void testGetDates() throws Exception { + mockMvc.perform(get("/function-call").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("functionCall.html")); + } + +} From 5015fe1d0cf822931f74fa48f2a6da8dd6528bee Mon Sep 17 00:00:00 2001 From: exaucae Date: Fri, 17 Jun 2022 19:58:12 +0000 Subject: [PATCH 3/4] feat(wip): refine examples --- .../controller/FunctionCallController.java | 4 +++- .../main/webapp/WEB-INF/js/functionCall.js | 8 +++++++ .../webapp/WEB-INF/views/functionCall.html | 24 +++++++++---------- 3 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/js/functionCall.js diff --git a/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FunctionCallController.java b/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FunctionCallController.java index b1869aa707..751dbc4e9d 100644 --- a/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FunctionCallController.java +++ b/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FunctionCallController.java @@ -1,5 +1,6 @@ package com.baeldung.thymeleaf.controller; +import com.baeldung.thymeleaf.utils.StudentUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @@ -10,7 +11,8 @@ public class FunctionCallController { @RequestMapping(value = "/function-call", method = RequestMethod.GET) public String getExampleHTML(Model model) { - model.addAttribute("num", 2); + model.addAttribute("totalStudents", StudentUtils.buildStudents().size()); + model.addAttribute("student", StudentUtils.buildStudents().get(0)); return "functionCall.html"; } } diff --git a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/js/functionCall.js b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/js/functionCall.js new file mode 100644 index 0000000000..41a71a73d2 --- /dev/null +++ b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/js/functionCall.js @@ -0,0 +1,8 @@ + +function greetWorld() { + alert("hello world") +} + +function salute(name) { + alert("hello: " + name) +} \ No newline at end of file diff --git a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html index 38555c3d2b..b3172a0c98 100644 --- a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html +++ b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html @@ -4,25 +4,23 @@ Thymeleaf: Javascript function call +
Thymeleaf: Javascript function call
-
-
-
Inline function call
-
-
Without a variable
- -
-
-
With a variable
- -
+
+ + + + + + +
- - \ No newline at end of file From ac5f74edc24670d1c016612dc4447d20e09aaefc Mon Sep 17 00:00:00 2001 From: exaucae Date: Fri, 17 Jun 2022 22:31:32 +0000 Subject: [PATCH 4/4] refactor: refine examples --- .../src/main/webapp/WEB-INF/js/functionCall.js | 8 -------- .../webapp/WEB-INF/views/functionCall.html | 18 ++++++++++++------ 2 files changed, 12 insertions(+), 14 deletions(-) delete mode 100644 spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/js/functionCall.js diff --git a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/js/functionCall.js b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/js/functionCall.js deleted file mode 100644 index 41a71a73d2..0000000000 --- a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/js/functionCall.js +++ /dev/null @@ -1,8 +0,0 @@ - -function greetWorld() { - alert("hello world") -} - -function salute(name) { - alert("hello: " + name) -} \ No newline at end of file diff --git a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html index b3172a0c98..d21fb7cf27 100644 --- a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html +++ b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html @@ -4,9 +4,16 @@ Thymeleaf: Javascript function call - +
Thymeleaf: Javascript function call
@@ -14,11 +21,10 @@
- - - + + + -