cy
2022-06-22 425675051e544cf29b2132615cfbf7a93dc5e51f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cn.ksource.core.web;
 
import java.util.ArrayList;
import java.util.List;
 
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
 
import cn.ksource.core.component.Format;
import cn.ksource.core.component.K_IFEmpty;
import cn.ksource.core.component.K_IFTrue;
import cn.ksource.core.component.LeftMethodModel;
 
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.TemplateHashModel;
 
public class MyFreeMarkerConfigurer extends FreeMarkerConfigurer {
 
    /**
     * 自动加载组件
     * @param cfg
     * @version V1.0.0
     * @author 杨凯
     * @date Dec 3, 2013 3:25:13 PM
     */
    private void loadAutoIncludeList(Configuration cfg) {
        
        //日期格式化
        cfg.setSharedVariable("format", new Format());
        cfg.setSharedVariable("left", new LeftMethodModel());
        cfg.setSharedVariable("K_IfEmpty", new K_IFEmpty());
        cfg.setSharedVariable("K_IfTrue", new K_IFTrue());
        
        
        List autoIncludeList = new ArrayList();
        autoIncludeList.add("/component/static_file.html");
        autoIncludeList.add("/component/formui.html");
        autoIncludeList.add("/component/Option.html");
        autoIncludeList.add("/component/tabs.html");
        autoIncludeList.add("/component/select.html");
        cfg.setAutoIncludes(autoIncludeList);
    }
    
    
    private void loadCommonStaticClass(Configuration cfg) {
        try {
            BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
            TemplateHashModel staticModels = wrapper.getStaticModels();
            cfg.setSharedVariable("FreeMarkerUtil", (TemplateHashModel) staticModels.get("cn.ksource.core.util.FreeMarkerUtil"));
            cfg.setSharedVariable("SysConstants", (TemplateHashModel) staticModels.get("cn.ksource.web.SysConstants"));
            cfg.setSharedVariable("SysConfigConstants", (TemplateHashModel) staticModels.get("cn.ksource.config.SysConfigConstants"));
            cfg.setSharedVariable("Constants", (TemplateHashModel) staticModels.get("cn.ksource.web.Constants"));
            cfg.setSharedVariable("GnConstants", (TemplateHashModel) staticModels.get("cn.ksource.web.GnConstants"));
            cfg.setSharedVariable("StringUtil", (TemplateHashModel) staticModels.get("cn.ksource.core.util.StringUtil"));
            cfg.setSharedVariable("DateUtil", (TemplateHashModel) staticModels.get("cn.ksource.core.util.DateUtil"));
            cfg.setSharedVariable("FullTextConstants", (TemplateHashModel) staticModels.get("cn.ksource.core.fulltext.FullTextConstants"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public Configuration getConfiguration() {
        Configuration cfg =  super.getConfiguration();
        //禁止本地化寻找
        cfg.setLocalizedLookup(false);
        cfg.setDefaultEncoding("utf-8");
        cfg.setClassicCompatible(true);
        //使用[#]替代<#ftl>
        cfg.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX);
        
        loadAutoIncludeList(cfg);
        loadCommonStaticClass(cfg);
        return cfg;
    }
    
}