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>
分类:
Java编程学习
标签:
freemarker与thymeleaf
, 前后端不分离
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义