Skip to content

Commit 23a8a4d

Browse files
Merge pull request eugenp#12372 from exaucae/BAEL-3825-Javascript-function-call-with-Thymeleaf
BAEL-3825: Javascript function call with Thymeleaf
2 parents fdd8397 + 39c0517 commit 23a8a4d

File tree

4 files changed

+110
-4
lines changed

4 files changed

+110
-4
lines changed

spring-web-modules/spring-thymeleaf/pom.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,9 @@
115115
</plugin>
116116
<plugin>
117117
<groupId>org.codehaus.cargo</groupId>
118-
<artifactId>cargo-maven2-plugin</artifactId>
119-
<version>${cargo-maven2-plugin.version}</version>
118+
<artifactId>cargo-maven3-plugin</artifactId>
119+
<version>${cargo-maven3-plugin.version}</version>
120120
<configuration>
121-
<wait>true</wait>
122121
<container>
123122
<containerId>jetty9x</containerId>
124123
<type>embedded</type>
@@ -143,7 +142,7 @@
143142
<javax.validation-version>2.0.1.Final</javax.validation-version>
144143
<hibernate-validator.version>6.0.11.Final</hibernate-validator.version>
145144
<!-- Maven plugins -->
146-
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
145+
<cargo-maven3-plugin.version>1.9.9</cargo-maven3-plugin.version>
147146
</properties>
148147

149148
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.thymeleaf.controller;
2+
3+
import com.baeldung.thymeleaf.utils.StudentUtils;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
9+
@Controller
10+
public class FunctionCallController {
11+
12+
@RequestMapping(value = "/function-call", method = RequestMethod.GET)
13+
public String getExampleHTML(Model model) {
14+
model.addAttribute("totalStudents", StudentUtils.buildStudents().size());
15+
model.addAttribute("student", StudentUtils.buildStudents().get(0));
16+
return "functionCall.html";
17+
}
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Thymeleaf: Javascript function call</title>
6+
</head>
7+
<script th:inline="javascript">
8+
function greetWorld() {
9+
alert("hello world")
10+
}
11+
12+
function salute(name) {
13+
alert("hello: " + name)
14+
}
15+
</script>
16+
17+
<body>
18+
<header>
19+
<div> Thymeleaf: Javascript function call </div>
20+
</header>
21+
<main>
22+
<section class="flex-box">
23+
<button th:onclick="greetWorld()">using no variable</button>
24+
<button th:onclick="'alert(\'static variable used here.\');'">using static variable</button>
25+
<button th:onclick="'alert(\'There are exactly ' + ${totalStudents} + ' students\');'">using inline dynamic variable</button>
26+
<button th:onclick="'javascript:alert(\'There are exactly ' + ${totalStudents} + ' students\');'">using javascript:function</button>
27+
<button th:data-name="${student.name}" th:onclick="salute(this.getAttribute('data-name'))">using data attribute</button>
28+
<button th:onclick="salute([[${student.name}]])">using double brackets</button>
29+
</section>
30+
</main>
31+
</body>
32+
</html>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.thymeleaf.controller;
2+
3+
import com.baeldung.thymeleaf.config.InitSecurity;
4+
import com.baeldung.thymeleaf.config.WebApp;
5+
import com.baeldung.thymeleaf.config.WebMVCConfig;
6+
import com.baeldung.thymeleaf.config.WebMVCSecurity;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.mock.web.MockHttpSession;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14+
import org.springframework.test.context.web.WebAppConfiguration;
15+
import org.springframework.test.web.servlet.MockMvc;
16+
import org.springframework.test.web.servlet.request.RequestPostProcessor;
17+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
18+
import org.springframework.web.context.WebApplicationContext;
19+
20+
import javax.servlet.Filter;
21+
22+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
23+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
24+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
25+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
26+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
27+
28+
@RunWith(SpringJUnit4ClassRunner.class)
29+
@WebAppConfiguration
30+
@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class })
31+
public class FunctionCallIntegrationTest {
32+
33+
@Autowired
34+
WebApplicationContext wac;
35+
@Autowired
36+
MockHttpSession session;
37+
38+
private MockMvc mockMvc;
39+
40+
@Autowired
41+
private Filter springSecurityFilterChain;
42+
43+
private RequestPostProcessor testUser() {
44+
return user("user1").password("user1Pass").roles("USER");
45+
}
46+
47+
@Before
48+
public void setup() {
49+
mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build();
50+
}
51+
52+
@Test
53+
public void testGetDates() throws Exception {
54+
mockMvc.perform(get("/function-call").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("functionCall.html"));
55+
}
56+
57+
}

0 commit comments

Comments
 (0)