ofbiz中FreeMarkerWorker的makeConfiguration方法

来源:互联网 发布:潮州美工培训 编辑:程序博客网 时间:2024/05/18 03:00

            这个方法是说明了为什么在ftl中可以使用一些java方法

            1.代码展示

public static Configuration makeConfiguration(BeansWrapper wrapper) {        /**         * freemarker.template.Configuration实例并调整其设置。         * 一个Configuration实例是存储FreeMarker应用程序级别设置的中心。         * 另外,它处理预先解析的模板(即 对象)的创建和 缓存Template         * */        Configuration newConfig = new Configuration(version);        /**         * @author jack         *         * 对象包装器         * wrapper == >freemarker.ext.beans.BeansWrapper         * 这是一个最原始的对象包装器,主要用来映射java         * 虽然原始,但是也有使用的时候,比如collection-s和map-s被允许修改模板时执行         * 参考资料 http://freemarker.org/docs/pgui_misc_beanwrapper.html         * */        newConfig.setObjectWrapper(wrapper);        /**         * @author jack         *         * 从beanswrapper返回TemplateHashModel。         * getstaticmodels()可以用来访问静态方法和任意一类的字段创建哈希模型。         * */        TemplateHashModel staticModels = wrapper.getStaticModels();        /**         * @author jack         * 将TemplateHashModel通过Static注入 以后就可以直接通过Static进行访问         * Shared variables共享变量是为所有模板定义的变量         * 形式:statics["java.lang.System"].currentTimeMillis() 这是一种调用java方法的处理方式 ftl中的用法         * */        newConfig.setSharedVariable("Static", staticModels);        /**         * @author jack         *         * #assign ls = EntityQuery.use(delegator).from("DictType").()     ftl中的用法         * 注入后就可以直接使用EntityQuery了         * */        try {            newConfig.setSharedVariable("EntityQuery", staticModels.get("com.hanlin.fadp.entity.util.EntityQuery"));        } catch (TemplateModelException e) {            Debug.logError(e, module);        }        /**         * @author jack         *         * 当一个模板包含另一个模板时,它试图加载以相同的本地化环境加载模板。         * 假定你的模板以本地化en_US来加载,那就意味着是U.S. English。当你包含另外一个模板:那么引擎实际上就会寻找一些模板,并按照这个顺序:         * footer_en_US.ftl         * footer_en.ftl         * footer.ftl         * 设置成为false就不会有这些问题         * */        newConfig.setLocalizedLookup(false);        //创建StringUtil这个工具类共享变量        newConfig.setSharedVariable("StringUtil", new BeanModel(StringUtil.INSTANCE, wrapper));        /**         * @author jack         *         * 如果在这些内建的模版加载器中没有一个符合你的要求,         * 那么你可以自己定制一个模版加载器,只需要实现freemarker.cache.TemplateLoader 接口就可以了,         * 然后通过方法setTemplateLoader(TemplateLoader loader)把其传递给Configuration对象。         * 主要业务处理不是很清楚         * */        newConfig.setTemplateLoader(new FlexibleTemplateLoader());        /**         * @author jack         *         * 导入库也就是说,它创建一个新的空命名空间 然后执行path在该命名空间中使用参数给出的模板         * 导入法则:         * #import "/lib/example.ftl" as e         * <@e.copyright date="1999-2002"/>         * 属性文件中的模板就是通过这种方式加载进去         * 所以在调用的时候需要加入命令空间         * */        newConfig.setAutoImports(UtilProperties.getProperties("freemarkerImports"));        /**         * @author jack         *         * 自定义类实现TemplateExceptionHandler         * 当ftl渲染出现异常调用这个类的handleTemplateException         * */        newConfig.setTemplateExceptionHandler(new FreeMarkerWorker.OFBizTemplateExceptionHandler());        try {            newConfig.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS");            newConfig.setSetting("number_format", "0.##########");        } catch (TemplateException e) {            Debug.logError("Unable to set date/time and number formats in FreeMarker: " + e, module);        }        // Transforms properties file set up as key=transform name, property=transform class name        /**         * @author jack         *         * 获取上下文加载器,当前加载器在webapp,随意加载其中config的freemarkerTransforms.properties所有值         * */        ClassLoader loader = Thread.currentThread().getContextClassLoader();        Enumeration<URL> resources;        try {            resources = loader.getResources("freemarkerTransforms.properties");        } catch (IOException e) {            Debug.logError(e, "Could not load list of freemarkerTransforms.properties", module);            throw UtilMisc.initCause(new InternalError(e.getMessage()), e);        }        /**         * @author jack         *         * 创建其中资源文件值得实例并通过key用setSharedVariable设置进入共享变量         * */        while (resources.hasMoreElements()) {            URL propertyURL = resources.nextElement();            Debug.logInfo("loading properties: " + propertyURL, module);            Properties props = UtilProperties.getProperties(propertyURL);            if (UtilValidate.isEmpty(props)) {                Debug.logError("Unable to locate properties file " + propertyURL, module);            } else {                loadTransforms(loader, props, newConfig);            }        }        return newConfig;    }
            2.用例说明

            2.1static           

<#assign displayApps = Static["org.ofbiz.webapp.control.LoginWorker"].getAppBarWebInfos(security, userLogin, ofbizServerName, "main")>
           2.2StringUtil

 <link rel="shortcut icon" href="<@ofbizContentUrl>${StringUtil.wrapString(shortcutIcon)}</@ofbizContentUrl>" />



          

原创粉丝点击