java里面我们需要使用模板的地方是比较多的,所以在freemarker官网查找资料整理了一个demo.
直接上代码
maven依赖
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
编写帮助类
package com.mqs.util;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.util.Map;
import java.io.StringWriter;
public class FreemarkerUtils {
private static String defaultCharacter = "UTF-8";
private static Configuration cfg;
private FreemarkerUtils() {
}
static {
cfg = new Configuration();
cfg.setDefaultEncoding(defaultCharacter);
cfg.setTagSyntax(