开源项目

知识点

相关文章

更多

最近更新

更多

Spring Boot CLI 基于Thymeleaf的示例

2019-04-29 23:18|来源: 网路

创建一个基于Thymeleaf的示例项目,以演示Spring CLI的功能。 按照下面提到的步骤创建一个示例项目 -

第1步

D:/worksp/springboot-cli/目录下,创建一个名称为TestApplication的文件夹,并在这个文件中创建两个子目录:statictemplates

第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 656463.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>

运行该应用程序

输入以下命令 -

D:/worksp/springboot-cli/TestApplication/> spring run *.groovy

现在,Spring Boot CLI将开始运行,下载所需的依赖项,运行嵌入式tomcat,部署应用程序并启动它。可以在控制台上看到以下输出 -

Resolving dependencies.............................

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _> | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.5.RELEASE)

...
2018-11-08 16:27:28.300  INFO 8360 --- [       runner-0] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-11-08 16:27:28.305  INFO 8360 --- [       runner-0] o.s.boot.SpringApplication

在浏览器中浏览应用程序

基于Spring应用现已准备就绪,打开网址为“http://localhost:8080/”,将看到以下输出 -

Go to Message

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

Message: Welcome to 656463.Com!

理解关键执行过程

以下操作由Spring CLI执行 -

  • 所有依赖项JAR仅在第一次使用时下载。

  • Spring CLI根据代码中使用的类和注释自动检测要下载的依赖项JAR。

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

相关问答

更多
  • 配置文件有问题吧,看下spring boot的yml配置里面的thymeleaf的配置
  • spring boot cli 弹簧启动CLI spring 英[sprɪŋ] 美[sprɪŋ] n. 春季; 泉水,小溪; 弹簧,弹性; 跳跃; vi. 跳,跃; 突然发出或出现; 发源; 劈开,裂开; vt. 突然跳出; 跳过; 使开裂; [例句]The Labor government of Western Australia has an election due next spring 西澳大利亚工党政府明年春天要举行选举。 [其他] 第三人称单数:springs 复数:springs 现在分词: ...
  • spring
  • 配置文件有问题吧,看下spring boot的yml配置里面的thymeleaf的配置
  • 它已经发布了一段时间,但希望这对其他人有所帮助。截至目前,Springboot无法自动配置Thymeleaf 3.0。 您将需要排除ThymeleafAutoConfiguration类并提供自定义配置。 您可以在以下网址中参考以下文档: https://gist.github.com/seanhinkley/6eab2130ceea857c160b 布局方言不是由Thymeleaf Team管理的,它仍然是一个悬而未决的问题。 https://github.com/ultraq/thymeleaf-lay ...
  • Web技术与桌面技术不同。 计算机是分开的:您的Web浏览器位于您的计算机上,并且该网页位于服务器上。 单击按钮时,Web浏览器会创建TCP / IP“Internet”数据包并将其发送到服务器,以便Web应用程序可以决定如何处理它。 在Java / AWT桌面应用程序中,使用ActionListeners,单击按钮最终会调用正在计算机上运行的应用程序。 即使它可以通过“事件模式”实现这样做,它仍然只是在您的应用程序中调用该方法。 在Spring Framework中,应用程序使用Controller处理“ ...
  • 所以我认为我已经找到了问题的答案,通过考虑一些重要的方面,如: 代码可重用性 使用像Thymeleaf这样的框架的一大好处是,您可以通过使用片段轻松地重用您编写的代码(如果是TL)。 在纯HTML + JQuery中创建前端意味着每个页面都需要有一个单独的.html,除非我们构建单个页面应用程序,但在这种情况下使用Angular会更有效率。 安全 我没有考虑的另一个方面; Thymeleaf与Spring Security兼容,它还具有在运行时对用户隐藏的特定语法。 如果服务器是REST服务器(就像在使用S ...
  • 例如,Spring Boot 2.0.2.RELEASE 文件WebConfig.java // File /src/main/java/com/donhuvy/WebConfig.java package com.donhuvy; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.Bean; import org.spri ...
  • 它自己说的错误就足够了:在类com.example.Journal没有创建createdAsShort这样的字段。 根据你所展示的,你班上肯定没有这样的领域。 The error it self says enough: there is no such field createdAsShort in class com.example.Journal. According to what you've shown definitely there is no such field in your clas ...