Merge pull request #12372 from exaucae/BAEL-3825-Javascript-function-call-with-Thymeleaf

BAEL-3825: Javascript function call with Thymeleaf
This commit is contained in:
davidmartinezbarua 2022-06-24 10:55:36 -03:00 committed by GitHub
commit 0215d8ee11
4 changed files with 110 additions and 4 deletions

View File

@ -115,10 +115,9 @@
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.codehaus.cargo</groupId> <groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId> <artifactId>cargo-maven3-plugin</artifactId>
<version>${cargo-maven2-plugin.version}</version> <version>${cargo-maven3-plugin.version}</version>
<configuration> <configuration>
<wait>true</wait>
<container> <container>
<containerId>jetty9x</containerId> <containerId>jetty9x</containerId>
<type>embedded</type> <type>embedded</type>
@ -143,7 +142,7 @@
<javax.validation-version>2.0.1.Final</javax.validation-version> <javax.validation-version>2.0.1.Final</javax.validation-version>
<hibernate-validator.version>6.0.11.Final</hibernate-validator.version> <hibernate-validator.version>6.0.11.Final</hibernate-validator.version>
<!-- Maven plugins --> <!-- Maven plugins -->
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version> <cargo-maven3-plugin.version>1.9.9</cargo-maven3-plugin.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,18 @@
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;
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("totalStudents", StudentUtils.buildStudents().size());
model.addAttribute("student", StudentUtils.buildStudents().get(0));
return "functionCall.html";
}
}

View File

@ -0,0 +1,32 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>Thymeleaf: Javascript function call</title>
</head>
<script th:inline="javascript">
function greetWorld() {
alert("hello world")
}
function salute(name) {
alert("hello: " + name)
}
</script>
<body>
<header>
<div> Thymeleaf: Javascript function call </div>
</header>
<main>
<section class="flex-box">
<button th:onclick="greetWorld()">using no variable</button>
<button th:onclick="'alert(\'static variable used here.\');'">using static variable</button>
<button th:onclick="'alert(\'There are exactly ' + ${totalStudents} + ' students\');'">using inline dynamic variable</button>
<button th:onclick="'javascript:alert(\'There are exactly ' + ${totalStudents} + ' students\');'">using javascript:function</button>
<button th:data-name="${student.name}" th:onclick="salute(this.getAttribute('data-name'))">using data attribute</button>
<button th:onclick="salute([[${student.name}]])">using double brackets</button>
</section>
</main>
</body>
</html>

View File

@ -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"));
}
}