3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Thyemeleafの簡単な実装

Last updated at Posted at 2015-06-15

環境

OS X Yosemite 10.10.3
jdk1.8.0_45.jdk
MySQL Server version: 5.5.28 Source distribution

Spring Tool Suite

Version: 3.6.4.RELEASE
Build Id: 201503100337
Platform: Eclipse Kepler SR2 (4.3.2)

Gradle IDE plugin Version: 3.6.4.201503050952-RELEASE
Groovy-Eclipse plugin Version: 2.9.2.xx-201502282108-e43j8

前提条件

下の状態から、

Groovyを使ったSpring Boot, Data JPA, MySQL操作の簡単な実装
http://qiita.com/quwahara/items/f4b1d30855fff83da3b8
https://github.com/quwahara/GP/tree/spring-jpa-groovy

余計なものを整理した下の状態に、Thymeleafの実装を加えます。

主な変更点として

Controllerとmainメソッドが一緒が嫌だったので、SampleController.groovyを削除して、mainメソッドのみのApplication.groovyを代わりに加えました。

src/main/java/hello/Application.groovy
package hello;

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.context.annotation.ComponentScan

@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

Thymeleafの依存をbuild.gradleに設定する

前提条件のbuild.gradleに// Addを追記

build.gradle

// 省略

dependencies {
    compile 'mysql:mysql-connector-java'
    compile 'org.codehaus.groovy:groovy-all'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    // Add
    compile 'org.springframework.boot:spring-boot-starter-thymeleaf'
    compile 'org.springframework.boot:spring-boot-starter-web'
}

// 省略

Package ExplorerでProjectのRoot要素を右クリック、Gradle → Refresh All

Template(View)を追加

src/main/resources/templatesディレクトリを作成します。
注意としてそれがないと、起動がエラーになります。
そのディレクトリ下にmemo_one.htmlを作ります。

src/main/resources/templates/memo_one.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
<title>memo one</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
  <div th:if="${memo}" th:object="${memo}">
    <div th:text="*{text}"></div>
    <div th:text="*{creationDate}"></div>
  </div>
  <div th:unless="${memo}">
    <div>No memo</div>
  </div>
</body>
</html>

Controllerを追加

src/main/java/hello/controller/MemoController.groovy
package hello.controller

import hello.domain.Memo
import hello.service.MemoRepository

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
// Add
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
// Add
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody


@Controller
class MemoController {
    // 省略

    // Add
    @RequestMapping(value="/memo/one", method=RequestMethod.GET)
    String one(Model model) {
        model.addAttribute("memo", memoRepository.findOne(1L))
        return "memo_one"
    }
}

application.propertiesにThyemeleafのcache無効化を設定

開発中はReloadする度に、htmlが更新された方が嬉しいので無効にします。

src/main/resources/application.properties
# 省略

# Add
# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.cache=false

アプリケーションを実行

Package Explorerでhello/Application.groovyを選択、右クリック
Debug As → Spring Boot App

ブラウザで下のURLを開く
http://localhost:8080/memo/new?text=one
http://localhost:8080/memo/one

参考

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?