springboot整合freemarker

springboot “天生”支持freemarker模板,创建好springboot项目后,resources下面会又一个templates文件夹,默认使用该文件夹下的模板,下面来看一下简单的引入配置。

#设置freemarker模板后缀
spring.freemarker.suffix=.html
#修改模板默认文件夹
#spring.freemarker.template-loader-path=classpath:/web

freemarker默认的模板后缀为 .ftl  为了方便在eclipse内编辑,现改为 .html

可通过spring.freemarker.template-loader-path=classpath:/web 修改系统默认的 模板存放路径

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>hello,freemarker</h3>
<h4>${str}</h4>
</body>
</html>
package com.example.demo.controller;

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

@Controller
@RequestMapping("/student/")
public class StudentController {
    
    @RequestMapping("show")
    public String show(Model model) {
        model.addAttribute("str", "this is spring boot freemarker");
        return "show";
    }

}
原文地址:https://www.cnblogs.com/blog411032/p/10339141.html