Java:模板引擎FreeMarker

简介: Java:模板引擎FreeMarker

image.png

依赖


<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.31</version>
</dependency>

使用示例


package com.pengshiyu;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class FreemarkerDemo {
    public static void main(String[] args) throws Exception {
        // 第一步:配置freemarker
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        // 设置模板路径
        configuration.setDirectoryForTemplateLoading(new File("./"));
        // 第二步:加载模板
        Template template = configuration.getTemplate("template.ftl");
        // 第三步:模板数据
        Map<String, Object> data = new HashMap<>();
        data.put("name", "汤姆");
        data.put("age", 23);
        // 第四步:渲染输出
        // 输出到字符串
        Writer stringWriter = new StringWriter();
        template.process(data, stringWriter);
        stringWriter.close();
        System.out.println(stringWriter.toString());
        // 输出到文件
        Writer fileWriter = new FileWriter(new File("hello.html"));
        template.process(data, fileWriter);
        fileWriter.close();
    }
}

模板文件 template.ftl


<html>
    <head>
        <title>Demo</title>
    </head>
    <body>
        <p>${name}</p>
        <p>${age}</p>
    </body>
</html>

输出结果 hello.html


<html>
    <head>
        <title>Demo</title>
    </head>
    <body>
        <p>汤姆</p>
        <p>23</p>
    </body>
</html>

问题及解决

报错


严重: DefaultObjectWrapper.incompatibleImprovements was set to the object returned by Configuration.getVersion(). That defeats the purpose of incompatibleImprovements, and makes upgrading FreeMarker a potentially breaking change. Also, this probably won't be allowed starting from 2.4.0. Instead, set incompatibleImprovements to the highest concrete version that's known to be compatible with your application.

解决方式


// 不推荐使用
Configuration configuration = new Configuration(Configuration.getVersion());
// 修改为:
Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);

模板语法

<#-- 注释 取出变量 -->
Hello ${name}
<#-- if判断-->
<#if name == "Tom">
    <span>is Tom</span>
<#elseif name == "Jack">
    <span>is Jack</span>
<#else>
    <span>not is Tom</span>
</#if>
<#--for循环-->
<#list list as item>
    <p>${item.name} ${item.age}</p>
</#list>
<#-- 引入模板 -->
<#include "./footer.html">
相关文章
|
2月前
Exception in thread “main“ java.lang.NoClassDefFoundError: freemarker/template/Configuration
Exception in thread “main“ java.lang.NoClassDefFoundError: freemarker/template/Configuration
55 0
|
XML Java 数据格式
使用Java + Freemarker 导出word文档
使用Java + Freemarker 导出word文档
|
12月前
|
数据采集 XML 缓存
【Java】SpringBoot项目整合FreeMarker加快页面访问速度
【Java】SpringBoot项目整合FreeMarker加快页面访问速度
169 0
|
10月前
|
SQL 开发框架 前端开发
低代码探索:Java模板引擎技术
本篇将探索代码生成技术。因为业务开发中使用Java语言较多,所以这里以Java作为背景语言。
132 0
|
安全 Java API
Java审计之Freemarker模板注入漏洞
Java审计之Freemarker模板注入漏洞
10439 1
|
Java PHP Python
Java:SpringBoot 整合 Pebble模板引擎渲染html
Java:SpringBoot 整合 Pebble模板引擎渲染html
203 0
Java:SpringBoot 整合 Pebble模板引擎渲染html
|
Java Spring
Java:SpringBoot 整合 Thymeleaf模板引擎渲染html
Java:SpringBoot 整合 Thymeleaf模板引擎渲染html
171 0
Java:SpringBoot 整合 Thymeleaf模板引擎渲染html
|
Java
Java:SpringBoot 整合 Freemarker模板引擎渲染html
Java:SpringBoot 整合 Freemarker模板引擎渲染html
250 0
Java:SpringBoot 整合 Freemarker模板引擎渲染html
|
SQL 小程序 关系型数据库
Java版点餐小程序2021最新版笔记,springboot+Mysql+freemarker+微信小程序实现扫码点餐小程序(上)
Java版点餐小程序2021最新版笔记,springboot+Mysql+freemarker+微信小程序实现扫码点餐小程序
185 0
|
前端开发 Java
Java入门007~springboot+freemarker+bootstrap快速实现分页功能
Java入门007~springboot+freemarker+bootstrap快速实现分页功能
121 0