目录

Starter Thymeleaf项目(Starter Thymeleaf Project)

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

Sr.No 步骤和说明
1 使用子文件夹templatesstatic创建名为TestApplication的文件夹。
2TestApplication文件夹中创建message.groovy ,在templates文件夹中创建message.groovy ,在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 iowiki.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)

在浏览器中浏览应用程序

我们基于弹簧的休息应用现已准备就绪。 打开网址为“ http://localhost:8080/ ”,您将看到以下输出 -

Go to Message

单击消息链接,您将看到以下输出 -

Message − Welcome to iowiki.com!

重要的几点 (Important points)

请考虑以下几点以了解Spring CLI采取的操作 -

  • @Grab('spring-boot-starter-thymeleaf')注释指示CLI下载spring-boot-starter-thymeleaf 1.5.8.RELEASE版本。

  • Spring CLI使用其元数据自动检测版本,因为我们在此处未指定任何组ID或版本ID。

  • 最后在代码编译之后,在嵌入式tomcat上部署war,在默认端口8080上启动嵌入式tomcat服务器。

↑回到顶部↑
WIKI教程 @2018