栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合前端框架Freemarker与Thymeleaf

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合前端框架Freemarker与Thymeleaf

整合Freemarker 创建SpringBoot项目时选择依赖的模板引擎

或者通过pom引入依赖

	org.springframework.boot
	spring-boot-starter-freemarker
    2.6.8
controller中通过Model向前端传值
@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/index")
    public String index(Model model) {
        // 模拟数据库取到的数据
        List users = new ArrayList<>();
        users.add(new User("1", "Lily", 18));
        users.add(new User("2", "Lisi", 21));
        users.add(new User("3", "Tom", 19));
        users.add(new User("4", "Tony", 20));
        model.addAttribute("users", users);
        return "user";
    }
}
在配置文件中定义Freemarker文件后缀

resources/application.properties

server.port=8081
spring.freemarker.suffix=.ftl
前端页面通过Freemarker语法读取后台传的数据

resources/templates/user.ftl


    
        
        用户列表
    
    
        
            <#list users as user>
            
ID 姓名 年龄
${user.id} ${user.name} ${user.age}
访问api测试


整合Thymeleaf 创建SpringBoot项目时选择依赖的模板引擎

引入依赖

    org.springframework.boot
    spring-boot-starter-thymeleaf
    2.6.8
controller中通过Model向前端传值
@Controller
public class HelloController {
    @RequestMapping("/index")
    public String index(Model model) {
        model.addAttribute("msg", "Hello, this is test Thymeleaf");
        return "index"; //指定返回给index.html页面
    }
}
前端页面通过Thymeleaf语法取值

resources/templates/index.html




    
    Title


    Thymeleaf testing
    

访问api测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/972894.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号