码迷,mamicode.com
首页 > 其他好文 > 详细

freemarker依赖

时间:2019-07-31 16:47:06      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:string   rect   style   obj   try   locale   --   ati   temp   

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<resource>
<directory>src/main/java</directory>
<includes>

<include>**/*.ftl</include>
</includes>
<!-- activate spring profile -->
<filtering>true</filtering>
</resource>
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<String,Object> 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();
    }

}

 

freemarker依赖

标签:string   rect   style   obj   try   locale   --   ati   temp   

原文地址:https://www.cnblogs.com/zfzf1/p/11277268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!