
- Spring Boot CLI 教程
- Spring Boot CLI - 主頁
- Spring Boot CLI - 概述
- Spring Boot CLI - 環境設定
- Spring Boot CLI - Hello World 示例
- Spring Boot CLI - “grab” 依賴關係推斷
- Spring Boot CLI - “grab” 協同關係推斷
- Spring Boot CLI - 預設宣告
- Spring Boot CLI - Thymeleaf 入門專案
- Spring Boot CLI - 打包應用程式
- Spring Boot CLI - 建立專案
- Spring Boot CLI - 使用 Shell
- springbootcli 有用資源
- Spring Boot CLI - 快速指南
- Spring Boot CLI - 有用資源
- Spring Boot CLI - 討論
Spring Boot CLI - Thymeleaf 入門專案
讓我們建立一個基於 Thymeleaf 的示例專案,以展示 Spring CLI 的功能。按照下列提到的步驟建立一個示例專案。
步驟 | 描述 |
---|---|
1 | 建立名稱為 TestApplication 的資料夾,其中包含子資料夾 templates 和 static。 |
2 | 在 TestApplication 資料夾中建立 message.groovy,在 templates 資料夾中建立 message.html,在 static 資料夾中建立 index.html,如以下所述。 |
3 | 編譯並執行應用程式以驗證已實現邏輯的結果。 |
TestApplication/message.groovy
@Controller @Grab('spring-boot-starter-thymeleaf') class MessageController { @RequestMapping("/message") String getMessage(Model model) { String message = "Welcome to TutorialsPoint.Com!"; model.addAttribute("message", message); return "message"; } }
TestApplication/templates/message.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Spring Boot CLI Example</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'Message: ' + ${message}" /> </body> </html>
TestApplication/static/index.html
<!DOCTYPE HTML> <html> <head> <title>Spring Boot CLI Example</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p>Go to <a href="/message">Message</a></p> </body> </html>
執行應用程式
輸入以下命令
E:/Test/TestApplication/> spring run *.groovy
現在 Spring Boot CLI 將開始發揮作用,下載所需的依賴關係,執行內嵌 tomcat,部署應用程式,並啟動應用程式。你可以在控制檯上看到以下輸出。
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.6.3) 2022-02-03 11:36:33.362 INFO 10764 --- [ runner-0] o.s.boot.SpringApplication : Starting application using Java 11.0.11 on DESKTOP-86KD9FC with PID 10764 (started by intel in E:\Test\TestApplication) 2022-02-03 11:36:33.372 INFO 10764 --- [ runner-0] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default 2022-02-03 11:36:35.743 INFO 10764 --- [ runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-02-03 11:36:35.768 INFO 10764 --- [ runner-0] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-02-03 11:36:35.769 INFO 10764 --- [ runner-0] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56] 2022-02-03 11:36:35.829 INFO 10764 --- [ runner-0] org.apache.catalina.loader.WebappLoader : Unknown class loader [org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@553a3d88] of class [class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader] 2022-02-03 11:36:35.896 INFO 10764 --- [ runner-0] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-02-03 11:36:35.897 INFO 10764 --- [ runner-0] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2085 ms 2022-02-03 11:36:36.436 INFO 10764 --- [ runner-0] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [static/index.html] 2022-02-03 11:36:37.132 INFO 10764 --- [ runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-02-03 11:36:37.153 INFO 10764 --- [ runner-0] o.s.boot.SpringApplication : Started application in 4.805 seconds (JVM running for 8.633) 2022-02-03 11:37:03.049 INFO 10764 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2022-02-03 11:37:03.049 INFO 10764 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2022-02-03 11:37:03.054 INFO 10764 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms
在瀏覽器中瀏覽應用程式
我們的基於 Spring 的 rest 應用程式現在已準備就緒。開啟網址“https://:8080/”,你將看到以下輸出。
Go to Message
單擊 Message 連結,你將看到以下輸出。
Message: Welcome to TutorialsPoint.Com!
注意事項
Spring CLI 執行以下操作。
@Grab('spring-boot-starter-thymeleaf') 註解指示 CLI 下載 spring-boot-starter-thymeleaf 2.6.3 版本。
Spring CLI 自動使用其元資料檢測版本,因為我們在此未指定任何組 ID 或版本 ID。
最後,它編譯程式碼,在內嵌 tomcat 上部署 war,在預設埠 8080 上啟動內嵌 tomcat 伺服器。
廣告