Spring Boot CLI - Starter Thymeleaf项目

在本章中,我们将学习如何创建一个基于Thymeleaf的示例项目来演示Spring CLI的功能.按照下面提到的步骤创建一个示例项目 :

Sr.NoStep&说明
1创建一个带有名称的文件夹 TestApplication 包含子文件夹模板静态.
2 TestApplication 文件夹中创建 message.groovy message.html templates 文件夹中, index.html static 文件夹中,如下所述.
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 = "/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 &minus; 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服务器.