Thymeleaf的简单实现

环境替代您所提供的单个选项。

OS X Yosemite 10.10.3
jdk1.8.0_45.jdk
MySQL服务器版本:5.5.28 源码发布版本

春季工具套件

版本:3.6.4.RELEASE
构建编号:201503100337
平台:Eclipse Kepler SR2 (4.3.2)

Gradle IDE插件版本:3.6.4.201503050952-RELEASE
Groovy-Eclipse插件版本:2.9.2.xx-201502282108-e43j8

前提条件 (Qian ti tiao jian)

从下面的状态开始,

使用Groovy实现的简单Spring Boot、Data JPA、MySQL操作
http://qiita.com/quwahara/items/f4b1d30855fff83da3b8
https://github.com/quwahara/GP/tree/spring-jpa-groovy

在整理掉多餘的東西後,我們將新增Thymeleaf的實現。

作为主要变更内容

因为不喜欢控制器和主要方法一起存在,所以我删除了SampleController.groovy并将只包含主要方法的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);
    }
}

在build.gradle中设置Thymeleaf的依赖。

在前提条件的build.gradle文件中追加// Add注释。


// 省略

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中,右键点击项目的根元素,选择Gradle → 刷新所有。

添加模板(视图)。

请创建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>

添加控制器

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的缓存禁用。

在开发过程中,每次重新加载时,如果HTML被更新了,那就让它无效,这样会更加令人高兴。

# 省略

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

执行应用程序

在包资源管理器中选择hello/Application.groovy文件,右击,
选择“以Spring Boot App方式调试”。

在浏览器中打开以下URL:
http://localhost:8080/memo/new?text=one
http://localhost:8080/memo/one

请提供更多上下文信息。

    • Source

 

    https://github.com/quwahara/GP/tree/spring-thymeleaf-groovy