如下创建了名为ctest_thymeleaf项目
二、导入坐标在pox.xml加入thymeleaf坐标
org.springframework.boot spring-boot-starter-parent 2.6.5 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf
一定要如下图标刷新
最后项目结构图:
在resources下创建一个templates目录,在该目录下创建hello.html.
th:text=“key” 会将从控制器相应的key对应的value放置在该标签文本内
thymeleaf和html不同的是多了一行xmlns:th="http://www.thymeleaf.org
在hello.html加入如下代码:
Title
控制器类HelloController:
package com.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping("/h")
public String he(Model model){
model.addAttribute("sayHello","Hello Thymeleaf!");
return "hello"; //返回 指定哪一个模板, 这个是上述的hello.html
}
}
SpringBoot启动类DemoApplication:
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
运行DemoApplication
在浏览器输入http://localhost:8080/h



