在本章中,我们将学习如何创建一个基于Thymeleaf的示例项目来演示Spring CLI的功能.按照下面提到的步骤创建一个示例项目 :
Sr.No | Step&说明 |
---|---|
1 | 创建一个带有名称的文件夹 TestApplication 包含子文件夹模板和静态. |
2 | 在 TestApplication 文件夹中创建 message.groovy , message.html 在 templates 文件夹中, index.html 在 static 文件夹中,如下所述. |
3 | 编译并运行应用程序以验证实现逻辑的结果. |
@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"; } }
<!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>
<!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 = "/msg">Message</a></p> </body> </html>
要运行应用程序,请键入以下命令 :
E:/Test/TestApplication/> spring run *.groovy
现在,Spring Boot CLI将开始运行,下载所需的依赖项,运行嵌入式tomcat,部署应用程序并启动它.您可以在控制台上看到以下输出 :
Resolving dependencies............................. . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _> | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.8.RELEASE) ... 2017-11-08 16:27:28.300 INFO 8360 --- [ runner-0] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2017-11-08 16:27:28.305 INFO 8360 --- [ runner-0] o.s.boot.SpringApplication : Started application in 4.203 seconds (JVM running for 38.792)
我们的基于Spring的休息应用程序现已准备就绪.打开网址为" http://localhost:8080/",您将看到以下输出 :
Go to Message
单击消息链接,您将看到以下输出 :
Message − Welcome to TutorialsPoint.Com!
请考虑以下几点,以了解Spring CLI采取的措施 :
@Grab('spring-boot-starter-thymeleaf')注释指示CLI下载spring-boot-starter-thymeleaf 1.5.8 .RELEASE版本.
Spring CLI会自动使用其元数据检测版本,因为我们在此处未指定任何组ID或版本ID.
最后在代码编译之后,在嵌入式tomcat上部署war,在默认端口8080上启动嵌入式tomcat服务器.