SpringBoot学习-(6)使用thymeleaf

thymeleaf是什么?是一个模板!
什么模板,看看再说。

要使用thymeleaf,需要先引入thymeleaf的依赖
1.依赖
pom.xml

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

2.添加配置文件
application.properties

#thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

3.创建文件夹及页面
这里写图片描述

thymeleaf.html内容如下:

<h1>i am first thymeleaf page</h1>

4.创建controller

package com.tang.controller;

import java.util.Map;

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

@Controller
@RequestMapping(value="/templates")
public class TemplateController {


    @RequestMapping(value="/thymeleaf")
    public String thymeleafTemplate(Map<String,Object> map){

        map.put("mname", "zhangsana");

        return "thymeleaf";
    }
}

5.启动服务测试
http://localhost:8080/springboot/templates/thymeleaf
这里写图片描述

这里面需要注意:在thymeleaf模板文件中,所有的标签需要闭合。

看了上面的内容,发现thymeleaf有点类似于springmvc。只是这里面我们使用的是html文件,而springmvc使用的是jsp页面。

猜你喜欢

转载自blog.csdn.net/jinjin603/article/details/79059708