freemark与thymeleaf依赖
这两个依赖主要用于前后端不分离的情况,是一种模板语言,前端页面不变的用HTML显示,动态数据则用这两个依赖传输
1. 导入依赖
//freemarker
implementation 'org.springframework.boot:spring-boot-starter-freemarker:2.7.5'
//thymeleaf
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf:2.7.5'
2. 配置文件
freemarker:
freemarker:
#模版后缀名
suffix: .ftl
#文档类型
content-type: text/html
#页面编码
charset: UTF-8
#页面缓存
cache: false
#模板路径
template-loader-path: classpath:/templates/
thymeleaf:
thymeleaf:
#设置路径
prefix: classpath:/templates/
#文件后缀
suffix: .html
#编码方式
encoding: utf-8
#文件类型
mode: HTML
#缓存
cache: false
3.Controller类
下面两处我用的是thymeleaf,所以freemarker将用截图
freemarker:

thymeleaf:
import com.example.springboot.domain.Test;
import com.example.springboot.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.validation.Valid;
@Validated
@Controller
public class TestController {
@GetMapping("/a")
public String b(Model model){
model.addAttribute("name","fff");
return "index";
}
}
4. HTML界面
freemarker:

thymeleaf:
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--取出name值-->
<h1 th:text="${name}">
</h1>
<h2>hello springboot</h2>
</body>
</html>

浙公网安备 33010602011771号