Skip to content

集成FreeMarker

1、pom.xml

xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

2、application.properties

properties
#### 是否开启thymeleaf缓存,本地为false,生产建议为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.allow-request-override=false
spring.freemarker.check-template-location=true
    
#类型
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
    
#文件后缀
spring.freemarker.suffix=.ftl
#路径
spring.freemarker.template-loader-path=classpath:/templates/

3、xxxController.java

java
@RequestMapping("/fm/index")
    public String fmIndex(ModelMap modelMap) {
        Map<String, String> map = new HashMap<>();
        map.put("name", "aoppp");
        map.put("desc", "描述");
        // 添加属性 给模版
        modelMap.addAttribute("user", map);
        return "fm/index";
    }

4、templates/fm/index.ftl

html
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h1>Test</h1>
    <span>name ===</span><span> ${user.name} </span>
    <span>desc ===</span><span> ${user.desc} </span>
</body>
</html>