huruyosi’s blog

プログラミングとかインフラとかのメモです。

spring boot その3 - テンプレートエンジンの Thymeleaf を組み込む

実装したソースは https://github.com/huruyosiathatena/springboot/tree/db743062dad8411906e959d00e3326c4249ca50f にあります。

view に Thymeleaf を使う

Tthymeleaf を利用して、 前回( spring boot その2 - bootstrapを組み込んで静的なページを表示する - huruyosi’s blog )の静的ファイルと同等の表示を行います。

pom.xml に依存関係を追加

dependencies に追加します。

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

コントローラーにアクションを追加

HelloControllerに thymeleaf メソッドを追加します。 メソッドの戻り値が org.springframework.web.servlet.ModelAndView になり テンプレートの情報を渡します。

   @RequestMapping("/thymeleaf")
    public ModelAndView thymeleaf() {
        Map<String, String> model = new HashMap<String, String>();
        model.put("message", "hello world");
        return new ModelAndView("HelloController/thymeleaf", model);
    }
  • ModelAndView のコンストラクタの第1引数にテンプレートファイルを指定します。
  • ModelAndView のコンストラクタの第2引数にテンプレートのパラメータを指定します。

テンプレートファイルを作成

テンプレートファイルは src/main/resources/templates の下におきます。テンプレートファイルは コントローラーのクラス名のディレクトリを作成し、その下に置きます。今回は src/main/resources/templates/HelloController/thymeleaf.html になります。

テンプレートの<span th:text="${message}">param1 content</span> にアクションの model.put("message", "hello world"); で設定したhello worldが表示されます。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" 
xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
  <head>
    <meta charset="utf-8" /> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="" />
    <meta name="author" content="" />

    <title>Starter Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet" />

    <!-- Custom styles for this template -->
    <link href="starter-template.css" rel="stylesheet" />

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="/assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="/assets/js/ie-emulation-modes-warning.js"></script>

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Project name</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container">

      <div class="starter-template">
        <h1>using template ending Thymeleaf </h1>
        <p class="lead"><span th:text="${message}">param1 content</span></p>
      </div>

    </div><!-- /.container -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="/bootstrap/js/bootstrap.min.js"></script>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="/assets/js/ie10-viewport-bug-workaround.js"></script>
  </body>
</html>

ホットデブロイとか thymeleaf のキャッシュ

ホットデブロイ

model.put("message", "hello world");hello worldを変更した後に画面への表示に反映させるためには再起動を行う必要があります。それでは時間がかかるので eclipseデバッグから起動すると、ホットデブロイの状態になります。

thymeleaf のキャッシュ

thymeleaf の変更も再起動が必要なので、対策を行います。

thymeleaf のキャッシュを無効にするために application.yml に spring.thymeleaf.cache: falseと定義します。開発中にだけキャッシュを無効にするために 開発用のファイルを作成します。

application.yml

spring:
  profiles.active: dev

application-dev.yml

spring:
    thymeleaf.cache: false

qiita.com

ファイルを追加した状態のプロジェクト

f:id:huruyosi:20150726093424p:plain

src/main/resoruce にテンプレートファイルとapplication.ymlを追加しました。