Skip to content

Commit 26a8c00

Browse files
committed
feat(wip): refine examples
1 parent 07a8c94 commit 26a8c00

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/FunctionCallController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.thymeleaf.controller;
22

3+
import com.baeldung.thymeleaf.utils.StudentUtils;
34
import org.springframework.stereotype.Controller;
45
import org.springframework.ui.Model;
56
import org.springframework.web.bind.annotation.RequestMapping;
@@ -10,7 +11,8 @@ public class FunctionCallController {
1011

1112
@RequestMapping(value = "/function-call", method = RequestMethod.GET)
1213
public String getExampleHTML(Model model) {
13-
model.addAttribute("num", 2);
14+
model.addAttribute("totalStudents", StudentUtils.buildStudents().size());
15+
model.addAttribute("student", StudentUtils.buildStudents().get(0));
1416
return "functionCall.html";
1517
}
1618
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
function greetWorld() {
3+
alert("hello world")
4+
}
5+
6+
function salute(name) {
7+
alert("hello: " + name)
8+
}

spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/functionCall.html

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@
44
<meta charset="UTF-8" />
55
<title>Thymeleaf: Javascript function call</title>
66
</head>
7+
<script type="text/javascript"
8+
src="../js/functionCall.js">
9+
</script>
710
<body>
811
<header>
912
<div> Thymeleaf: Javascript function call </div>
1013
</header>
11-
1214
<main>
13-
<section>
14-
<div>Inline function call</div>
15-
<div>
16-
<header>Without a variable</header>
17-
<button th:onclick="'alert(\'a\');'">Without variable</button>
18-
</div>
19-
<div>
20-
<header>With a variable</header>
21-
<button th:onclick="'alert(\'' + ${num} + '\');'">With variable</button>
22-
</div>
15+
<section class="flex-box">
16+
<button th:onclick="greetWorld()">using no variable</button>
17+
<button th:onclick="'alert(\' 1. static variable used here.\');'">using static variable</button>
18+
<button th:onclick="'alert(\' '1. There are exactly + ${totalStudents} + students'\');'">using inline dynamic variable</button>
19+
<button th:onclick="'javascript:alert(\''2. There are exactly + ${totalStudents} + students'\');'">using javascript:function</button>
20+
<button th:data-name="${student.name}" th:onclick="salute(this.getAttribute('data-name'))">using data attribute</button>
21+
<button th:onclick="@{'salute(' + ${student.name} + ');'}">using @ tag</button>
22+
<button th:onclick="salute([[${student.name}]])">using double brackets</button>
2323
</section>
2424
</main>
25-
26-
2725
</body>
2826
</html>

0 commit comments

Comments
 (0)