ofbiz中FreeMarkerWorker的makeConfiguration方法

            這個方法是說明了爲什麼在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>" />



          

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章