首页 > 其他 > 详细

Thymeleaf 入门

时间:2019-02-27 17:12:19      阅读:187      评论:0      收藏:0      [点我收藏+]

基本项目结构:

技术分享图片

 

Thymeleaf配置:

spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/static/
spring.thymeleaf.suffix=.html

spring默认的模板映射路径是:src/main/resources/templates 

 ftl路径的hello.html

<!DOCTYPE html>
<html lang="zh-CN"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

    <head>
        <title>helloWorld</title>
    </head>

    <body>
        <h1>Hello : <b th:text="${name}">姓名</b></h1>
    </body>
</html>

 TestController:

 

package com.eshore.ismp.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class TestController {
	@RequestMapping(value = "test", method = RequestMethod.GET)
    public String test(Model model) {
        model.addAttribute("name", "MERCY");
        return "/ftl/hello";
    }

}

运行结果:

技术分享图片

 

 

 要注意的点:

spring.thymeleaf.cache要设置为true,以方便开发及时看到页面刷新

springboot直接引入:

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

非springboot项目引入:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>2.1.4</version>
</dependency>
LEGACYHTML5属性值需要搭配一个额外的库NekoHTML才可用。主要作用是设置Thymeleaf格式检查没这么严格,默认属性值是HTML5

  

  

 

Thymeleaf 入门

原文:https://www.cnblogs.com/JAYIT/p/10444763.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!