`
zenius
  • 浏览: 55822 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Velocity Sample

    博客分类:
  • 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 org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;

import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

import java.io.*;
import java.util.ArrayList;

/**
 * This class is a simple demonstration of how the Velocity Template Engine
 * can be used in a standalone application.
 *
 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
 * @version $Id: Example.java 463298 2006-10-12 16:10:32Z henning $
 */

public class Example
{
    public Example()
    {
        try
        {
            /*
             * setup
             */

            //Velocity.init("velocity.properties");

            /*
             *  Make a context object and populate with the data.  This
             *  is where the Velocity engine gets the data to resolve the
             *  references (ex. $list) in the template
             */

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

            /*
             *  get the Template object.  This is the parsed version of your
             *  template input file.  Note that getTemplate() can throw
             *   ResourceNotFoundException : if it doesn't find the template
             *   ParseErrorException : if there is something wrong with the VTL
             *   Exception : if something else goes wrong (this is generally
             *        indicative of as serious problem...)
             */

            Template template =  null;

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

            /*
             *  Now have the template engine process your template using the
             *  data placed into the context.  Think of it as a  'merge'
             *  of the template and the data to produce the output stream.
             */

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

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

            /*
             *  flush and cleanup
             */

            writer.flush();
            writer.close();
        }
        catch( Exception e )
        {
            System.out.println(e);
        }
    }

    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)
    {
        Example t = new Example();
    }
}


## 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.    

#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



分享到:
评论

相关推荐

    java swing开发一款代码生成工具swing-generate,可通过velocity模板引擎在线定制模板

    4. `sample`目录:可能包含示例模板和数据,供用户参考学习。 5. `lib`目录:可能包含工具运行所依赖的外部库,如Velocity引擎的JAR文件。 使用`swing-generate`,开发者可以显著提高代码编写效率,特别是在处理...

    webapp_sample:一个基于Velocity+SpringMVC+Spring+MyBatis架构的网站示例

    这是一个基于Velocity+SpringMVC+Spring+MyBatis架构的网站示例。 1.生成工程的步骤(前提jdk,eclipse,gradle都已安装配置完毕): 1)创建webapp目录,然后编写build.gradle 2)运行gradle createWebProject命令,...

    blade-sample

    Blade 默认使用 Velocity 模板引擎,但也可以通过配置使用其他模板引擎。 ## 六、依赖注入与服务组件 Blade 提供了依赖注入功能,你可以通过 `@Inject` 注解来注入服务组件。在 `sample` 包下,可能会有一个 `...

    JavaGen-Sample-Code

    模板可以是简单的文本文件,也可以是更复杂的模板引擎如FreeMarker或Velocity的模板。 3. **数据模型的创建**:在生成代码之前,需要定义数据模型,这通常是Java类或对象,它们包含了代码生成所需的信息,如类名、...

    Android代码-BattleSats

    the initial velocity. Pinch to zoom in/out. Battle Sats is written in Java (using Eclipse). It uses some bits from the Lunar Lander sample included with the Android SDK. It is available under the New ...

    SQL2JAVA

    2. `velocity-dep-1.3.1.jar`和`velocity-1.3.1.jar`:这两个是Velocity模板引擎的库文件,用于动态生成Java源代码。 3. `hsqldb.jar`:这是一个轻量级的嵌入式SQL数据库引擎,用于测试和演示目的。 4. `sql2java....

    Meteor_sample:使用Test Todo应用程序测试流星框架

    3. **Meteor 的测试框架**:Meteor 提供了自有的测试工具集,包括 Velocity 和 Meteor Testing Package。这些工具使得在 Meteor 应用中编写单元测试和集成测试变得简单。在 Test Todo 应用中,我们可以看到如何设置...

    Analysis of sampled fiber Bragg gratings in polarization-maintaining fiber

    A novel type of sampled fiber Bragg gratings (FBGs) written in polarization-maintaining fiber (PMF) is proposed. The reflection spectrum, time delay, and group velocity dispersion (GVD) of the ...

    ACC搭建步骤(英文).docx

    由于模型中使用了不同的采样频率,为了确保各个任务的同步,需要将“Tasking mode for periodic sample times”设置为“Single Tasking”。这确保了系统在一个单一的任务调度模式下运行,以处理不同时间间隔的数据。...

    pro Spring

    Become aware of the seamlessness and power of Spring by referencing the easy-to- understand sample application we provide. Learn how to replace common EJB features with Spring alternatives, including...

    Manning - Struts 2 in Action (May 2008).pdf 及源代码

    此外,Struts 2支持多种视图技术,如JSP、FreeMarker、Velocity等,提供了高度的视图解耦。 源代码通常包含了书中讲解的SampleApplication,这是一个典型的Struts 2应用实例,它展示了如何组织项目结构、配置Struts...

    java 通过模板生成 xml

    3. **模板引擎**:除了Java内置的API,还可以使用模板引擎如Freemarker或Velocity。这些模板引擎允许编写更高级的模板,其中包含逻辑和条件语句。例如,Freemarker示例: ```java import freemarker.template....

    接收机抽取的Matlab仿真代码-SFND_radar_target_generation_detection:SFND_radar_targ

    velocity. % delay time can be calculated from range and speed of light r_t(i) = range + v*t(i); td(i) = 2*r_t(i)/c; % *%TODO* : %For each time sample we need update the transmitted and %received

    Linear variable filters fabricated by ion beam etching with triangle-shaped mask and normal film coating technique

    During the ion etching, the sample is moved back and forth repeatedly with a constant velocity for the purpose of obtaining the linearly varied thickness of the cavity. Combined with ion beam ...

    jin-template:从 code.google.compjin-template 自动导出

    JSP 使用原始Java 代码,或者JSTL,Velocity 或Smarty 也使用特殊表达式等。jin-template 的目的是将视图构建逻辑与视图分离,并在模板端保留纯HTML。 jin-template 的解决方案非常简单:在 HTML(或 XML)中定义带...

    matlab画图 - Matlab Code Examples

    % initial velocity % end global parameters % check that stability condition is satisfied if(SR * f0) error('Stability condition violated'); end N = 1 / SR; % time step coef = 2 - N^2 * (2 * pi * ...

    Photoacoustic spectroscopy for fast and sensitive ammonia detection

    A new PA cell structure is designed to accelerate the drift velocity of the sample gas near the cell surface, so that the short response time at the flow rate of 100 sccm (standard cubic centimeter ...

Global site tag (gtag.js) - Google Analytics