Springbootcli-thymeleaf-project

提供:Dev Guides
移動先:案内検索

Spring Boot CLI-スターターThymeleafプロジェクト

この章では、サンプルのThymeleafベースのプロジェクトを作成して、Spring CLIの機能を示す方法を学習します。 サンプルプロジェクトを作成するには、以下の手順に従ってください-

Sr.No Step & Description
1 Create a Folder with a name TestApplication with subfolders templates and static.
2 Create message.groovy in TestApplication folder, messagel in templates folder, indexl in static folder as explained below.
3 Compile and run the application to verify the result of the implemented logic.

TestApplication/message.groovy

@Controller
@Grab('spring-boot-starter-thymeleaf')

class MessageController {
   @RequestMapping("/message")

   String getMessage(Model model) {
      String message = "Welcome to finddevguides.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)

ブラウザでアプリケーションを閲覧する

これで、スプリングベースの休息アプリケーションの準備ができました。 「 http://localhost:8080/ 」としてURLを開くと、次の出力が表示されます-

Go to Message

メッセージリンクをクリックすると、次の出力が表示されます-

Message − Welcome to finddevguides.Com!

重要なポイント

次の点を考慮して、Spring CLIで実行されるアクションを理解します-

  • @Grab( 'spring-boot-starter-thymeleaf')アノテーションは、CLIにspring-boot-starter-thymeleaf 1.5.8.RELEASEバージョンをダウンロードするよう指示します。
  • ここではグループIDまたはバージョンIDを指定していないため、Spring CLIはメタデータを使用してバージョンを自動的に検出します。
  • 最後に、コードのコンパイル後、組み込みTomcatにwarをデプロイし、デフォルトポート8080で組み込みTomcatサーバーを起動します。