feat(wip): add function call examples

This commit is contained in:
exaucae 2022-06-17 16:55:28 +00:00
parent cd033c2109
commit 7adea75450
3 changed files with 101 additions and 0 deletions

View File

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

View File

@ -0,0 +1,28 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>Thymeleaf: Javascript function call</title>
</head>
<body>
<header>
<div> Thymeleaf: Javascript function call </div>
</header>
<main>
<section>
<div>Inline function call</div>
<div>
<header>Without a variable</header>
<button th:onclick="'alert(\'a\');'">Without variable</button>
</div>
<div>
<header>With a variable</header>
<button th:onclick="'alert(\'' + ${num} + '\');'">With variable</button>
</div>
</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"));
}
}