File tree Expand file tree Collapse file tree 3 files changed +22
-14
lines changed
spring-web-modules/spring-thymeleaf/src/main
java/com/baeldung/thymeleaf/controller Expand file tree Collapse file tree 3 files changed +22
-14
lines changed Original file line number Diff line number Diff line change 11package com .baeldung .thymeleaf .controller ;
22
3+ import com .baeldung .thymeleaf .utils .StudentUtils ;
34import org .springframework .stereotype .Controller ;
45import org .springframework .ui .Model ;
56import 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}
Original file line number Diff line number Diff line change 1+
2+ function greetWorld ( ) {
3+ alert ( "hello world" )
4+ }
5+
6+ function salute ( name ) {
7+ alert ( "hello: " + name )
8+ }
Original file line number Diff line number Diff line change 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 >
You can’t perform that action at this time.
0 commit comments