欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

FreeMarker

程序员文章站 2022-06-06 10:32:31
...

FreeMarker介绍:

FreeMarker 是一款模板引擎:基于 模板 + 数据 => 输出文本(html文件,json文件,java文件等)。FreeMarker

入门案例:

  1. 导入依赖:
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
        </dependency>
    </dependencies>
  1. 配置文件:
server:
  port: 8088
spring:
  application:
    name: test-ferrmarker
  freemarker:
    cache: false #关闭模板缓存,方便测试
    settings:
      template_update_delay: 0 #检查模板更新延迟时间,设置为0表示立即检查,如果时间大于0会有缓存不方便进行模板测试
  1. Controller
@Controller
@RequestMapping("/freemarker")
public class FreemarkerController {
    @RequestMapping("/test1")
    public String test1(Map<String, Object> map) {
        map.put("name", "*鹏");
        return "test1";
    }
}
  1. template/test1.ftl
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
</head>
<body>
    Hello ${name}!
</body>
</html>
  1. 访问页面展示效果:
    FreeMarker
相关标签: freemarker