美文网首页
Spring Boot使用thymeleaf

Spring Boot使用thymeleaf

作者: 任重而道元 | 来源:发表于2017-06-25 11:27 被阅读155次

在pom.xml中引入thymeleaf

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

如何关闭thymeleaf缓存

 # Thymeleaf 模板引擎配置 
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html 
# 关闭缓存
spring.thymeleaf.cache=false 

编写模板文件.html

在templates文件夹下创建hello.html文件

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
    <h1>Hello,Thymeleaf</h1>
    <br />
    This is my first thymeleaf demo .
    
    <hr/>
    
    welcome <span th:text="${name}"></span>
</body>
</html>

编写访问模板文件controller

创建TemplatesController.java控制层文件

package com.example.demo.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 注意:
 * 1.在Themeleaf模板文件 中,标签是需要闭合的,3.0之前是需要闭合的
 * 2.themeleaf 3.0+是可以不强制要求闭合的
 * @author Lidy
 *
 */
@Controller
@RequestMapping("/templates")
public class TemplatesController {

    /**
     * 映射地址:/templates/hello
     * @return
     */
    @RequestMapping("/hello")
    public ModelAndView hello(Map<String,Object> map){
        ModelAndView mv = new ModelAndView("hello");
        map.put("name", "道哥");
        return mv;
    }
    
}

访问浏览器

Paste_Image.png

相关文章

网友评论

      本文标题:Spring Boot使用thymeleaf

      本文链接:https://www.haomeiwen.com/subject/wfglcxtx.html