Spring BootCLI-スターターThymeleafプロジェクト
この章では、SpringCLIの機能を示すためにサンプルのThymeleafベースのプロジェクトを作成する方法を学習します。サンプルプロジェクトを作成するには、以下の手順に従ってください-
| シニア番号 | ステップと説明 | 
|---|---|
| 1 | サブフォルダーテンプレートと静的なTestApplicationという名前のフォルダーを作成します。 | 
| 2 | 作成message.groovyのでTestApplicationの、フォルダmessage.htmlでテンプレートフォルダ、index.htmlをで静的以下に説明するようにフォルダを。 | 
| 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ベースのRESTアプリケーションの準備が整いました。URLを「http://localhost:8080/"と次の出力が表示されます-
Go to Message 
    メッセージリンクをクリックすると、次の出力が表示されます-
Message − Welcome to TutorialsPoint.Com! 
    重要なポイント
Spring CLIが実行するアクションを理解するには、次の点を考慮してください。
@Grab( 'spring-boot-starter-thymeleaf')アノテーションは、CLIにspring-boot-starter-thymeleaf1.5.8.RELEASEバージョンをダウンロードするように指示します。
ここではグループIDまたはバージョンIDを指定していないため、SpringCLIはメタデータを使用してバージョンを自動的に検出します。
最後に、コードのコンパイル後、組み込みのTomcatにwarをデプロイし、デフォルトのポート8080で組み込みのTomcatサーバーを起動します。