zoukankan      html  css  js  c++  java
  • FreeMarker辅助

    /**
     * FreeMarker 辅助类
     * @author Rubekid
     *
     */
    public class FreeMarkerHelper {
        
        /**
         * 模板文件存放目录
         */
        private static final String TEMPLATE_PATH = "/templates/freemarker/";
        
        /**
         * 获取模板内容
         * @param tpl
         * @param rootMap
         * @return
         * @throws IOException
         * @throws TemplateException
         */
        public static String fetch(String tpl, Object rootMap) throws IOException, TemplateException{
            Template template = getTemplate(tpl);
            Writer out= new StringWriter();
            template.process(rootMap, out);
            String result = out.toString();
            out.flush();
            return result;
        }
        
        /**
         * 输出显示
         * @param tpl
         * @param rootMap
         * @param out
         * @throws IOException
         * @throws TemplateException
         */
        public static void display(String  tpl, Object rootMap, Writer out) throws IOException, TemplateException{
            Template template = getTemplate(tpl);
            template.process(rootMap, out);
            out.flush();
        }
        
        /**
         * 获取模板
         * @param tpl
         * @return
         * @throws IOException
         */
        private static Template getTemplate(String tpl) throws IOException{
            Configuration config = new Configuration();
            String path = WebUtils.getContextPath() + TEMPLATE_PATH;
            String name =  tpl;
            int pos = tpl.lastIndexOf("/");
            if(pos > -1){
                path += tpl.substring( 0, pos+1 );
                name += tpl.substring( pos+1 );
            }
            
            config.setDirectoryForTemplateLoading(new File(path));
            config.setObjectWrapper(new DefaultObjectWrapper());
            return  config.getTemplate(name ,"utf-8");
        }
        
        /**
         * 获取模板(通过字符串)
         * @param source
         * @throws IOException 
         */
        private static Template getTemplate(String name, String source) throws IOException{
            Configuration config = new Configuration();
            StringTemplateLoader loader = new StringTemplateLoader();
            loader.putTemplate(name, source);
            config.setTemplateLoader(loader);
            return config.getTemplate(name, "utf-8");
        }
  • 相关阅读:
    Microsoft.Jet.Oledb.4.0 提供者並未登錄於本機電腦上
    asp.net将本地Excel上传到服务器并把数据导入到数据库
    报错:Cannot insert explicit value for identity column in table 't' when identity_insert is set to OFF
    select * from table where 1=1让您茅塞顿开(转)
    cessss
    从“黑掉Github”学Web安全开发
    聊聊JVM的年轻代
    Deployment options
    Put your application in production
    Manage application.conf in several environments
  • 原文地址:https://www.cnblogs.com/rubekid/p/3981153.html
Copyright © 2011-2022 走看看