• java VelocityEngine 属性key值得组成说明


    今天看了下模板类VelocityEngine得使用,代码如下:

       public static String testVelocityEngine() throws IOException {
            //初始化
            VelocityEngine ve = new VelocityEngine();
            ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
           //ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
            ve.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            ve.init();
    
            // 载入(获取)模板对象
            Template t = ve.getTemplate("HelloVelocity.vm");
    
            VelocityContext ctx = new VelocityContext();
            // 域对象加入参数值
            ctx.put("name", "best");
            ctx.put("date", (new Date()).toString());
                // list集合
                List list = new ArrayList();
                list.add("1111");list.add("2222");
                list.add("3333");list.add("4444");
            ctx.put("list", list);
    
            //内容打印
    //        StringWriter sw = new StringWriter();
    //        t.merge(ctx, sw);
    //        System.out.println(sw.toString());
            //写入文件
            File file = new File("E:\\MyWorkSpace\\StudySpring\\Study-Velocity\\src\\main\\resources\\out.txt");
            FileWriter writer = new FileWriter(file);
            t.merge(ctx, writer);
            writer.flush();
            writer.close();
            return "testVelocityEngine";
        }
    View Code

    Velocity得模板语法网上有很多文档,在此不做讨论! 本例模板如下:

    #set( $iAmVariable = "good!" )
    Welcome $name to velocity.com
    today is $date.
    #foreach ($i in $list)
        $i
    #end
    $iAmVariable
    View Code

    我的问题就是:在 ve.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 里,这个key ("classpath.resource.loader.class")是怎么来的?这个肯定不是乱写得,那如果我用其他得加载器,这个key值应该怎么填呢?毕竟还有很多其他类加载器(当然可能用不到,此处只做研究),如下图

    经过查看源代码,其实这个key是由三部分组成得:

    1.第一部分很显然,ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");决定key得第一部分内容:

    2.第二部分内容由 RuntimeConstants.RESOURCE_LOADER里得宏定义确定得

    3.第三部分,就得根据源代码里得获取情况去分别去看看源代码了

    下面是源码:

     

    此文件是通过RuntimeInstance里得initializeResourceManager()方法过来得,而此方法中

    String rm = getString(RuntimeConstants.RESOURCE_MANAGER_CLASS);
    其实走的是在前面截图里的默认配置velocity.properties,
    此处说明一下,希望大家不要迷惑,很多配置,在默认配置里其实已经有了,自行查看
    以上解决了1、2两点的由来,相对比较固定!


    第三部分源码如下:
    源码里,第三部分的key其实是写死的"class"/"instance",这些都是固定的:
    所以,我们如果设置一个ve.setProperty("classpath.resource.loader.instance", instance);
    那么ve.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");的设置其实是不生效的,大家自行查看源码吧,篇幅太长,我折叠了!
    到此,这三部分的由来基本说清楚了!

    下面是官网地址:https://velocity.apache.org/engine/1.6.2/ 这是1.6.2版本,大家可以查看其他版本的使用情况

    当然还有很多其他key值得使用情况,基本上可以通过查看源码、官网、默认配置这三个途径去了解!祝好运!






  • 相关阅读:
    python生成xml文件
    gcc基本用法
    Java删除文件或目录及目录下所有文件
    QTimer在QThread环境中失效的问题
    fopen打开文件失败的问题
    利用枚举,简化多个标志位统计
    QTableView表格自动拉伸
    django1.11 启动错误:Generator expression must be parenthesized
    css选择器
    property使用
  • 原文地址:https://www.cnblogs.com/ruber/p/16173590.html
Copyright © 2020-2023  润新知