您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页Spring Boot使用thymeleaf

Spring Boot使用thymeleaf

来源:二三四教育网

在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 
<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

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务