ã¯ããã«
ããã«ã¡ã¯ãæ°å2å¹´ç®ã®å²¡å´ã§ããã¨ããµã¤ãHDã¢ããã³ãã«ã¬ã³ãã¼2024ã®5æ¥ç®ãæ å½ãã¾ãã
ä»ã¾ã§ã®è¨äºã¯ãã¡ããã覧ãã ããã
ãã¦ãä»åã¯ãSpringBoot + Thymeleafã§ãJavaScriptã«å¤æ°ãåãè¾¼ãæ¹æ³ãç´¹ä»ãã¾ãã
ç°å¢
- Java 試ãã«23ã使ã£ã¦ãã¾ããã21ã§ãåé¡ãªãã§ãã
openjdk version "23.0.1" 2024-10-15 OpenJDK Runtime Environment Corretto-23.0.1.8.1 (build 23.0.1+8-FR) OpenJDK 64-Bit Server VM Corretto-23.0.1.8.1 (build 23.0.1+8-FR, mixed mode, sharing)
- gradle
------------------------------------------------------------ Gradle 8.11.1 ------------------------------------------------------------ Build time: 2024-11-20 16:56:46 UTC Revision: 481cb05a490e0ef9f8620f7873b83bd8a72e7c39 Kotlin: 2.0.20 Groovy: 3.0.22 Ant: Apache Ant(TM) version 1.10.14 compiled on August 16 2023 Launcher JVM: 23.0.1 (Amazon.com Inc. 23.0.1+8-FR) Daemon JVM: /Users/hiromi.okazaki/.sdkman/candidates/java/23.0.1-amzn (no JDK specified, using current Java home) OS: Mac OS X 14.7 aarch64
- SpringBoot
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.4.0)
ä¾åé¢ä¿
build.gradleã«ä»¥ä¸ã®è¨å®ããªãå ´åã¯ã追å ãã¦ãã ããã
dependencies { // SpringBootã®è¨å® implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' // SpringBootã§Thymeleafã使ãããã®è¨å® implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' }
å®è£
ä»åã®ç®æ¨ã¯ãControllerã§è¨å®ããå¤æ°ããã³ã³ã½ã¼ã«ã§è¡¨ç¤ºãããã¨ã§ãã
Controller
ã¾ãã¯ãControllerã§å¤æ°ã®è¨å®ãè¡ãã¾ãã
ä»åã¯sampleMessage
ã¨ããå¤æ°ã«ãhello world
ãå¤ã¨ãã¦ã»ãããã¾ããã
å®è£ ä¾ã¯ä»¥ä¸ã§ãã
@Controller @RequiredArgsConstructor @RequestMapping("sample") public class SampleController { @GetMapping public String getSample( Model model ) { model.addAttribute("sampleMessage", "hello world"); return "page/sample/index"; } }
html
次ã«htmlã®å®è£ ãè¡ãã¾ãã
resources以ä¸ããä»åã¯ä»¥ä¸ã®æ§æã«ãã¾ããã
resources | -- templates | -- pages | -- sample | -- index.html
index.htmlã®å®è£ ã¯ä»¥ä¸ã®éãã§ãã
<!DOCTYPE html> <html lang="ja" xmlns:th="http://www.thymeleaf.org"> <head> <title></title> <meta charset="UTF-8"> <script th:inline="javascript"> const sampleMessage = /*[[${sampleMessage}]]*/"sampleã§ã"; console.log(sampleMessage); </script> </head> <body> sample pageã§ã </body> </html>
å®è£
ã®è§£èª¬ã§ãã
ä»åã¯Thymeleafã使ããControllerã§ã¢ãã«ã«ã»ããããå¤ãã³ã³ã½ã¼ã«ã«è¡¨ç¤ºãã¾ãã
JavaScriptã§å¤æ°ã使ããããscriptã¿ã°ã«th:inline="javascript"
ãè¨å®ãã¾ãã
<script th:inline="javascript"> // ä¸ç¥ </script>
scriptã¿ã°ã®ä¸ã§å¤æ°ã使ãå ´åã/*[[${ }]]*/
ã®ä¸ã«å¤æ°ãã»ãããã¾ãã
ä»åã¯å¤æ°åãsampleMessageã¨ãã¦ããã®ã§ã/*[[${sampleMessage}]]*/
ã¨ãªãã¾ãã
sampleMessage
ã¯ã¤ãã¥ã¼ã¿ãã«ãªå¤ã¨ãããã£ãã®ã§ãconst
ã使ãã¾ããã
const sampleMessage = /*[[${sampleMessage}]]*/;
ã ãã ã¨ãåæå¤ããªãã¨å¤æãããããã'const' variable without initializer is not allowed
ã¨ããã¨ã©ã¼ã¡ãã»ã¼ã¸ãåºã¾ãã
ãããã£ã¦ãåæå¤ãã»ããããå¿
è¦ãããã¾ãã
/*[[${ }]]*/"ããã«åæå¤ãã»ãããã"
ã¨ããã°ãåæå¤ãè¨å®ã§ãã¾ãã
æå¾ã«ã¢ããªã±ã¼ã·ã§ã³ãèµ·åããæå®ããã¨ã³ããã¤ã³ãã«ã¢ã¯ã»ã¹ãã¾ãã
ãã¼ã¸ã表示ããã¾ããã ããã§ã³ã³ã½ã¼ã«ãéãã¦ç¢ºèªããã¨ãsampleMessageã«è¨å®ãããå¤ã表示ããã¾ããã
æå¾ã«
ä»åã¯ãSpringBoot + Thymeleafã§ãJavaScriptã«å¤æ°ãåãè¾¼ãæ¹æ³ãç´¹ä»ãã¾ããã ã©ãªããã®åèã«ãªãã°å¹¸ãã§ãã