`
kodak_zhou
  • 浏览: 135634 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

(转)velocity模板路径

阅读更多
遇到的velocity加载模板时的路径问题。

于是查阅资料解决。最后综合velocity自己带的例子的example1和example2,改写了一个例子。怎样解决的在例子的注释中已经说的很明确。对于初学velocity的同志来说,这个例子可以是你参照学习的良好实例

Java代码 
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.   
*/

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;

/**
* This class is a simple demonstration of how the Velocity Template Engine
* can be used in a standalone application using the Velocity utility class.
*
* It demonstrates two of the 'helper' methods found in the org.apache.velocity.util.Velocity
* class, mergeTemplate() and evaluate().
*
*
* @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
* @version $Id: Example2.java 463298 2006-10-12 16:10:32Z henning $
*/

public class Example2
{

public static ArrayList getNames()
    {
        ArrayList list = new ArrayList();

        list.add("ArrayList element 1");
        list.add("ArrayList element 2");
        list.add("ArrayList element 3");
        list.add("ArrayList element 4");

        return list;
    }

    public static void main( String args[] )
    {
        /* first, we init the runtime engine.  Defaults are fine. */

    Properties p = new Properties();
    //设置输入输出编码类型。和这次说的解决的问题无关
        p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
        //这里加载类路径里的模板而不是文件系统路径里的模板
        p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        //也可以用下面方法指定一个绝对路径,不过这样要求你所有的模板都放在该路径下,是有局限的
        //p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "模板路径");
        try
        {
            Velocity.init(p);
        }
        catch(Exception e)
        {
            System.out.println("Problem initializing Velocity : " + e );
            return;
        }

        /* lets make a Context and put data into it */

        VelocityContext context = new VelocityContext();

        context.put("name", "Velocity");
        context.put("project", "阿帕奇");
       
        context.put("list", getNames());

        /* lets render a template */

        StringWriter w = new StringWriter();

        try
        {
            Velocity.mergeTemplate("example2.vm", "UTF-8", context, w );
        }
        catch (Exception e )
        {
            System.out.println("Problem merging template : " + e );
        }

        System.out.println(" template : " + w );

        /*
         *  lets dynamically 'create' our template
         *  and use the evaluate() method to render it
         */

        //这个例子也同时告诉我们可以先从文件系统读取一个文件到字符串,然后进行我们想要的操作
        String s = "We are using $project $name to render this.";
        w = new StringWriter();

        try
        {
            Velocity.evaluate( context, w, "mystring", s );
        }
        catch( ParseErrorException pee )
        {
            /*
             * thrown if something is wrong with the
             * syntax of our template string
             */
            System.out.println("ParseErrorException : " + pee );
        }
        catch( MethodInvocationException mee )
        {
            /*
             *  thrown if a method of a reference
             *  called by the template
             *  throws an exception. That won't happen here
             *  as we aren't calling any methods in this
             *  example, but we have to catch them anyway
             */
            System.out.println("MethodInvocationException : " + mee );
        }
        catch( Exception e )
        {
            System.out.println("Exception : " + e );
        }

        System.out.println(" string : " + w );
       
        ///////////////////////////////////////////////////////
        //其他方法: 1分别指定路径,此方法可以设定不同的路径 (也可是相对的。在eclipse下是工程目录)
        try {
        VelocityEngine velocityEngine = new VelocityEngine();
        Properties properties = new Properties();
        //也可以在这里指定绝对路径。当指定相对路径时, 在不同的环境下是有区别的。
        //比如把程序部署到tomcat以后,相对路径相对到哪里是个很恶心的事情。
        String basePath = "vm";
        //可设置绝对路径
        //String basePath = "F:/";
        properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);
        velocityEngine.init(properties);
        Template template = velocityEngine.getTemplate("example2.vm");
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(System.out));
        template.merge(context, writer);
        writer.flush();
            writer.close();
        } catch (Exception e) {
        e.printStackTrace();
        }
    }
}
分享到:
评论

相关推荐

    velocity模板路径

    博文链接:https://zhyt710.iteye.com/blog/235250

    基于模板的代码生成器LKGenerator1.1.0_x86

    (1)配置模版路径,这个配置可以配置用于生成代码的velocity模版路径,生成的代码将模版中的特定符号见“模版中可使用的变量”中的描述。 (2)配置生成代码的固定文件路径,这个配置中的文件将原样拷贝到最终生成...

    基于模板的代码生成器LKGenerator1.1.0_x64

    (1)配置模版路径,这个配置可以配置用于生成代码的velocity模版路径,生成的代码将模版中的特定符号见“模版中可使用的变量”中的描述。 (2)配置生成代码的固定文件路径,这个配置中的文件将原样拷贝到最终生成...

    express-velocity:基于express搭建的velocity服务器

    express-velocity 基于express搭建的velocity服务器 使用方式 Command: $ git clone ... 'viewsPath': 'vm/', //模板文件路径 'staticPath': 'public' //静态资源文件路径 } 依赖

    javaweb项目,代码生成工具(Java源码)

    该项目为代码生成器 基于Apache Velocity的 Java模板引擎 base_build文件夹为最原始代码 为整理后的版本 执行: com.mmk.BaseApplication.java或生成jar包:run Maven intall 访问路径:...

    AutoEE_V1-自动智能开发平台免费下载

    AutoEE-自动智能开发平台是一款使用当前主流框架搭建,并结合Velocity模板引擎和MyBatis Generator实现的高度可自定义、高度可配置的自动智能生成代码的开源的快速开发平台。 使用AutoEE开发平台可以简单、快速、...

    word源码java-generate_code_manager:generate_code_manager

    word源码java generate_code_manager 项目介绍 我为什么想开源一个代码生成器? 现在的项目基本上属于前后端分离的 ...重写了velocity获取模板路径的方法,然后在自己项目中的模板 用velocity的语法写了能生成单张表

    独立与项目外部的javaweb代码生成器

    该项目为代码生成器 基于Apache Velocity的 Java模板引擎 base_build文件夹为最原始代码 为整理后的版本 执行: com.mmk.BaseApplication.java或生成jar包:run Maven intall 访问路径:...

    word源码java-code-generator:代码生成器

    通过Velocity模板引擎 实现代码的生成(java、xml、sql)支持CRUD下载 。 需要生成的数据库必须有完整的备注和库表注释,不然程序无法是识别 项目支持word、excel文件格式下载 项目支持批量生成代码,加快编程效率 ...

    NutzWk企业级开源开发框架 v5.0.1

    NutzWk是基于Nutz的Java开源企业级开发框架。 NutzWk是集成了Shiro权限控制、Ehcache缓存、Redis、Email服务、Quartz定时任务、Lucene搜索引擎、Beetl/Velocity模板引擎等技术的开源企业级开发框架。

    Struts2属性文件详解

    该属性指定视图主题所需要模板文件的位置, 该属性的默认值是template,即默认加载template路径下的模板文件. struts.ui.templateSuffix 该属性指定模板文件的后缀,该属性的默认属性值是ftl.该属性还允许使用ftl、vm...

    NutzWk企业级开源开发框架

    NutzWk是集成了Shiro权限控制、Ehcache缓存、Redis、Email服务、Quartz定时任务、Lucene搜索引擎、Beetl/Velocity模板引擎等技术的开源企业级开发框架。 NutzWk 3.x 运行环境: 1、JDK 8 2、Tomcat 8 3、Maven ...

    Struts2\constant应用

    该属性指定视图主题所需要模板文件的位置,该属性的默认值是template,即默认加载template路径下的模板文件。 struts.ui.templateSuffix 该属性指定模板文件的后缀,该属性的默认属性值是ftl。该属性还允许使用...

    Spring in Action(第2版)中文版

    14.4.1使用velocity模板 14.4.2使用freemarker 14.5产生非html输出 14.5.1产生excel工作表 14.5.2产生pdf文档 14.5.3开发自定义视图 14.6小结 第15章使用springwebflow 15.1开始springwebflow之旅 15.1.1...

    Spring in Action(第二版 中文高清版).part2

    14.4.1 使用Velocity模板 14.4.2 使用FreeMarker 14.5 产生非HTML输出 14.5.1 产生Excel工作表 14.5.2 产生PDF文档 14.5.3 开发自定义视图 14.6 小结 第15章 使用Spring Web Flow 15.1 开始Spring Web ...

    Spring in Action(第二版 中文高清版).part1

    14.4.1 使用Velocity模板 14.4.2 使用FreeMarker 14.5 产生非HTML输出 14.5.1 产生Excel工作表 14.5.2 产生PDF文档 14.5.3 开发自定义视图 14.6 小结 第15章 使用Spring Web Flow 15.1 开始Spring Web ...

    Struts2 in action中文版

    8.3.1 VelocityResult,也叫做velocity 189 8.3.2 FreemarkerResult,也叫做freemarker 191 8.4 全局结果 192 8.5 小结 193 第四部分 完善应用程序 第9章 集成Spring和Hibernate/JPA 196 9.1 为什么在Struts 2中...

    static-starter:静态项目启动器,包括 Gulp、Browserify、Sass、Bourbon、Neat、Bitters 和 Jade-ified html5 样板。 基于 gulp-starter

    它还包括 Velocity 和 jQuery 2.x(速度依赖)。 在安装之前,您可以轻松地从 package.json 中删除它们。安装 npm 依赖 npm install之后,只需运行default gulp 任务: gulp配置所有路径和插件设置都被抽象到gulp/...

    LaTeX:LaTeX渲染实用程序LaTeX渲染服务器(Spring Boot REST微服务)

    在Windows上的上安装Linux /在Linux上的上安装 ,并确保pdflatex可执行文件可通过系统路径使用(例如,通过在命令行上执行)。 浏览到latex-renderer/src/test/resources/META-INF并通过执行pdflatex sample.tex...

    springboot参考指南

    自定义管理服务器的上下文路径 iii. 41.3. 自定义管理服务器的端口 iv. 41.4. 自定义管理服务器的地址 v. 41.5. 禁用HTTP端点 vi. 41.6. HTTP Health端点访问限制 iv. 42. 基于JMX的监控和管理 i. 42.1. 自定义...

Global site tag (gtag.js) - Google Analytics