Springboot thymeleaf統合


Springboot 2のthymeleaf統合は比較的簡単で、以下の手順に従います.
  • pom.xmlにweb依存とthymeleaf依存を追加します.application.ymlは変更せずにデフォルトの構成を使用できます.
  • <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
    
  • srcmainjavacontrollerでIndexController
  • を作成
    @Controller
    public class IndexController {
        @RequestMapping("/index")
        public String index(ModelMap map) {
            map.put("text", "hello");
            map.put("href", "http://www.fengyunxiao.cn");
            return "index";
        }
    }
    
  • srcmainresourcestemplatesでindexを作成します.html
  • 
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Titletitle>
    head>
    <body>
    	<input th:id="${text}" th:name="${text}" th:value="${text}" />
    	<a th:href="${href}" th:text="${text}">    a>
    body>
    html>
    
  • プロジェクトを実行し、ブラウザにアドレスを入力して結果を表示:localhost:8080/index
  • 参照先:http://www.fengyunxiao.cn