`

初始velocity

 
阅读更多
Velocity是一个基于java的模板引擎。它允许任何人仅仅简单的使用模板语言来引用由java代码

定义的对象。

工程目录为:





example.vm内容如下:

#set( $this = "Velocity")

$this is great!

#foreach( $name in $list )
    $name is great!
#end

#set( $condition = true)

#if ($condition)
    The condition is true!
#else
    The condition is false!
#end




Example.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.    
 */

package com.test;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
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.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

public class Example
{
    public Example(String templateFile)
    {
        try
        {
            Properties p = new Properties() ;
            String basePath = "src/template";
            p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);
            Velocity.init(p);

            VelocityContext context = new VelocityContext();
            context.put("list", getNames());

            Template template =  null;

            try
            {
                template = Velocity.getTemplate(templateFile);
            }
            catch( ResourceNotFoundException rnfe )
            {
                System.out.println("Example : error : cannot find template " + templateFile );
            }
            catch( ParseErrorException pee )
            {
                System.out.println("Example : Syntax error in template " + templateFile + ":" + pee );
            }

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

            if ( template != null)
                template.merge(context, writer);

            writer.flush();
            writer.close();
        }
        catch( Exception e )
        {
            System.out.println(e);
        }
    }
    
    @SuppressWarnings("unchecked")
	public 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)
    {
        new Example("example.vm");
    }
}



运行结果如下:

Velocity is great!

    ArrayList element 1 is great!
    ArrayList element 2 is great!
    ArrayList element 3 is great!
    ArrayList element 4 is great!


    The condition is true!
  • 大小: 29.3 KB
分享到:
评论

相关推荐

    Velocity初始化过程解析

    **Velocity初始化过程解析** 在Java Web开发中,Apache Velocity是一个常用的模板引擎,它允许开发者将内容和呈现逻辑分离,提供了一种高效的动态内容生成方式。本文将深入解析Velocity的初始化过程,帮助开发者更...

    velocity入门使用教程

    然后,在Java代码中初始化Velocity引擎,并通过Velocity引擎读取模板文件,再将Java对象数据传入模板,最终合并生成最终的文本输出。 具体实现步骤如下: 1. 创建模板文件hello.vm,在文件中定义输出内容,比如...

    JAVAEE Velocity例子工程

    `VelocityTest` 文件可能是项目中的主要源代码文件,它可能包含了初始化Velocity上下文、加载模板和渲染输出的过程。例如,它可能有以下代码片段: ```java VelocityContext context = new VelocityContext(); ...

    velocity例子

    - 初始化Velocity:在Java代码中,使用`Velocity.init()`方法初始化Velocity引擎,并加载配置文件。 - 设置上下文:通过`VelocityContext`对象将Java对象添加到上下文中,以便在模板中使用。 - 渲染模板:调用`...

    velocity中文文档 教程

    1. **初始化Velocity上下文**:在Java代码中,你需要创建一个`VelocityContext`对象,并向其中添加你要在模板中使用的变量。 2. **加载模板**:使用`VelocityReader`或`ResourceLoader`加载模板文件。 3. **合并上...

    JAVA的Velocity语法学习

    3. 使用`#set`指令时,记得对变量进行初始化,防止空指针异常。 4. 利用Velocity提供的宏(macro)功能,可以创建可重用的代码片段,提高模板的复用性。 5. 调试和错误处理:使用Velocity的`#debug`指令可以帮助...

    Velocity之HelloWorld配置

    在Java代码中,我们需要初始化Velocity上下文(Context)和Velocity引擎。在`main`方法中,加入以下代码: ```java import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.VelocityContext; ...

    SpringBoot Velocity 代码生成模板

    Spring Boot简化了Spring的初始化和配置过程,而Velocity则是一个强大的模板引擎,常用于生成动态内容。 1. **SpringBoot基础**: Spring Boot是基于Spring框架的微服务开发工具,它内置了Tomcat服务器,简化了...

    Velocity介紹.ppt

    1. **初始化 Velocity**:调用 Velocity.init() 来初始化引擎,加载必要的配置。 2. **创建 Context 对象**:Context 对象是数据容器,用于存储要展示的数据。 3. **添加数据**:将 Java 对象放入 Context 中,...

    Velocity介绍.ppt

    1. **初始化 Velocity**:调用 Velocity.init() 来初始化 Velocity 环境。 2. **创建 Context 对象**:创建 VelocityContext 实例,它是一个键值对容器,用来存储要传递到模板的数据。 3. **添加数据到 Context**:...

    Mastering.Apache.Velocity

    - **初始化 Velocity**:通过 Velocity 的 API 初始化引擎,并配置相关的属性。 #### 七、Velocity 的高级功能 1. **国际化支持**:Velocity 支持多语言和国际化,可以方便地切换不同语言版本的模板。 2. **错误...

    velocity示例

    3. **初始化Velocity**:通过调用`Velocity.Init()`方法初始化Velocity引擎。 4. **获取模板**:使用`Velocity.GetTemplate`方法获取指定名称的模板。 5. **创建上下文对象**:创建一个`VelocityContext`对象用于...

    velocity-1.6.zip

    1. **创建引擎实例**:通过`org.apache.velocity.app.VelocityEngine`初始化Velocity引擎。 2. **设置配置**:配置Velocity,例如模板路径、缓存策略等。 3. **加载模板**:使用引擎加载模板文件。 4. **填充上下文*...

    Velocity_API

    首先,需要初始化 Velocity 运行时环境: ```java Properties props = new Properties(); props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem"); props.set...

    velocity经典4pdf中文教程

    它介绍了Velocity Engine的API,包括初始化、渲染模板、数据模型的构建等。同时,书中还会讲解如何利用Velocity进行模板测试,以及在大型项目中的性能优化策略。 通过学习这四本书,开发者不仅可以掌握Velocity的...

    velocity的简单demo

    在这个示例中,`VelocityDemo`类创建了一个`VelocityEngine`实例,初始化了`VelocityContext`并添加了两个变量:`name`和`color`。接着,它从资源文件夹中加载了`welcome.vm`模板,并使用`merge`方法将上下文中的...

    velocity 配置jar包

    2. **初始化 VelocityEngine**:创建 VelocityEngine 实例并配置其属性。例如: ```java VelocityEngine ve = new VelocityEngine(); ve.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "path/to/your/...

    Velocity模板入门DEMO

    通过`VelocityContext`和`VelocityEngine`类,可以在Java代码中初始化和配置Velocity。 在“Apache-Velocity-java”这个压缩包中,很可能包含了示例代码和一个简单的 Velocity 模板文件,展示如何在Java应用程序中...

Global site tag (gtag.js) - Google Analytics