0
点赞
收藏
分享

微信扫一扫

SpringBoot中整合Thymeleaf


场景

springboot不建议使用jsp,使用模板引擎,比如thymeleaf,velocity,freemarker。


实现

在项目中引入相关依赖

<!-- springboot整合thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

 

springboot会默认在resource下的templates下去寻找模板。

SpringBoot中整合Thymeleaf_spring

新建test.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 th:text="${name}"></h1>
</body>
</html>

在Controller包下新建TestThymeleaf .java

package com.example.demo.controller;

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

@Controller
public class TestFreeMarker {

@RequestMapping("/freemarker")
public String show(Model model) {
model.addAttribute("name","霸道流氓气质");
return "show";
}
}

运行效果

SpringBoot中整合Thymeleaf_spring_02


举报

相关推荐

0 条评论