-->

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:
image
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:
image

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>
posted @   ꧁ʚ星月天空ɞ꧂  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示