zoukankan      html  css  js  c++  java
  • FreeMarker(六)全局配置

    全局配置基本与有关freemarker.template.Configuration相关。

    /**
     * 较通用的FreeMarkers工具类
     *
     * @author ChenSS 2017-10-19
     * @date 2020-12-21 Mr.css 尝试使用ClassForTemplateLoading
     * @since FreeMarker Version2.3.23
     */
    public class FreeMarkers {
        private static Configuration config;
        private static final Version VERSION = new Version("2.3.23");
        private static final String FILE_ROOT = "tmp/";
    
        static {
            try {
                config = new Configuration(VERSION);
                config.setEncoding(P.LOCALE, Charset.defaultCharset().name());
                config.setNumberFormat("0.####");
                config.setBooleanFormat("true,false");
                config.setWhitespaceStripping(true);
                config.setClassicCompatible(true);
                config.setClassForTemplateLoading(ClassPathResource.class, FILE_ROOT);
            } catch (Exception e) {
                throw new SystemError("FreeMarker config error: ", e);
            }
        }
    }

    共享变量

    调用Configuration对象的setSharedVariable()方法即可。

        public static void main(String[] args) throws Exception {
            Map<String, Object> map = new HashMap<>();
            map.put("name", "xiaoming");
            Configuration cfg = FreeMarkers.defaultConfiguration();
            cfg.setSharedVariable("share", "共享变量");
            cfg.setSharedVaribles(map); 
    
            System.out.println(FreeMarkers.fileToStr("test1.ftl", map));
            System.out.println(FreeMarkers.fileToStr("test2.ftl", map));
        }

    全局的数据格式化配置

    调用Configuration特定函数即可。

        setNumberFormat("0.####");
        setDateFormat("yyyy-MM-dd");
        setTimeFormat("HH:mm:ss");
        setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
        setBooleanFormat("true,false");
        setWhitespaceStripping(true);
        setClassicCompatible(true);
        setDirectoryForTemplateLoading(File dir):设置模板存放路径——文件路径;
        setServletContextForTemplateLoading(Object sctxt, String path):设置模板存放路径——Servlet上下文;
        setClassForTemplateLoading(Class clazz, String pathPrefix):设置模板存放路径——类路径;
        setTemplateUpdateDelay(int delay):设置模板文件更新时间(毫秒);
        setStrictSyntaxMode(boolean b):开启/关闭严格的语法,默认为truesetWhitespaceStripping(boolean b):开启/关闭空白移除,默认为truesetTagSyntax(int tagSyntax):设置ftl标签的类型;
             ?AUTO_DETECT_TAG_SYNTAX: 可以自定义格式,系统会选择第一个标签作为参照;
             ?ANGLE_BRACKET_TAG_SYNTAX: 使用尖括号;
             ?SQUARE_BRACKET_TAG_SYNTAX: 使用方括号;
        setSharedVariable(String name, TemplateModel tm):设置共享变量;
        setSharedVariable(String name, Object obj):设置共享变量;
        setAllSharedVariables(TemplateHashModelEx hash):设置共享变量;
        clearSharedVariables():清除共享变量;
        clearTemplateCache():清除缓存;
        setSetting(String key, String value):为freemarker做配置属性的入口;
        getTemplate:一系列获取Template的方法;

    使用<#setting>标签数据格式化配置

    在文件中使用此标签做全局的设置

    // 用来设置整个系统的一个环境 
    locale // zh_CN 中文环境
    number_format 
    boolean_format 
    date_format
    time_format
    datetime_format 
    time_zone 
    classic_compatible
    // 例1:
    <#setting number_format="percent"/>    // 设置数字默认输出方式('percent',百分比)
    
    // 例2:
    // 假如当前是匈牙利的设置,然后修改成美国
    ${1.2} // 输出1,2
    <#setting locale="en_US"> 
    ${1.2} // 输出1.2,因为匈牙利是采用", "作为十进制的分隔符,美国是用". "
  • 相关阅读:
    js全局变量
    $.getJSON异步请求和同步请求
    让js中的函数只有一次有效调用
    两个div并排显示,当浏览器界面缩小时会出现换行
    jquery获取窗口和文档的高度和宽度
    后台传带引号(")的数据需要注意
    C# dynamic
    (转)数据库函数解析JSON字符串
    Unicode和UTF-8
    用户通过浏览器修改表单隐藏域
  • 原文地址:https://www.cnblogs.com/chenss15060100790/p/8538747.html
Copyright © 2011-2022 走看看