您的位置:首页 > 博客中心 > 互联网 >

freemarker依赖

时间:2022-05-08 08:09


org.freemarker
freemarker


org.springframework.boot
spring-boot-starter-web


ch.qos.logback
logback-classic



src/main/java


**/*.ftl


true
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;

public class FreeMarkerUtil {

    private final static Logger logger = LoggerFactory.getLogger(FreeMarkerUtil.class);

    private static Configuration conf = null;
    static {
        conf = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        conf.setClassForTemplateLoading(FreeMarkerUtil.class, "/com/yung/view");

        conf.setObjectWrapper(new DefaultObjectWrapper(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS));
        conf.setLocale(Locale.CHINA);
        conf.setDefaultEncoding("utf-8");
        conf.setClassicCompatible(true);
    }

    public static String getString(Map root,String tplName) {

        Writer out = new StringWriter(2048);
        try {
            Template temp = conf.getTemplate(tplName);
            temp.process(root, out);
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
        } catch (TemplateException e) {
            logger.error(e.getMessage(),e);
        }
        return out.toString();
    }

}

 

本类排行

今日推荐

热门手游