spring boot 集成thymeleaf引擎模板

198 阅读1分钟

1、添加jar包

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

2、配置文件设置

spring.thymeleaf.cache=falsespring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/htmlspring.thymeleaf.mode=LEGACYHTML5

3、java后台代码

@Controllerpublic class TestController {    @Autowired
    TestService testService;

    @RequestMapping("/")
    public String testThymeleaf(ModelMap modelMap){
        modelMap.addAttribute("msg", "helloworld");
        return "index";
    }

}

4、页面设计

必须要放指定的文件夹下,此处的路径是resource/templates,内容如下

<!DOCTYPE html><html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"><head><meta charset="UTF-8"><title>index</title></head><body><h1 th:text="${msg}"></h1></body></html>