首页 > 编程语言 > 详细

SpringBoot中使用Thymeleaf模板

时间:2019-09-09 17:32:20      阅读:85      评论:0      收藏:0      [点我收藏+]

1、引入pom依赖

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

 

2、设置thymeleaf版本,版本3检查html标签可以没有闭合结束符

    <properties>
        <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
    </properties>

3、配置文件设置thymeleaf属性

spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5

4、resources/templates新建index.html文件

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello World!</title>
</head>
<body>
    <h1 th:inline="text">Hello</h1>
    <p th:text="${hello}"></p>
</body>
</html>

5、编写controller

@RequestMapping
@Controller
public class IndexController {

    @GetMapping("/helloHtml")
    public String testHelloHtml(Model model){
        model.addAttribute("hello", "你好");
         return "index";
    } 
}

 

SpringBoot中使用Thymeleaf模板

原文:https://www.cnblogs.com/moris5013/p/11492716.html

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