Java/Velocity/Collections
Collection: List
/*
* Copyright 2000-2001,2004 The Apache Software Foundation.
*
* Licensed 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
== Collection: Map ==
<!-- start source code -->
<source lang="java">
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
public class MapDemo {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/mapProperty.vm");
VelocityContext ctx = new VelocityContext();
Map map = new HashMap();
map.put("firstName", "Joe");
map.put("lastName", "Wang");
ctx.put("map", map);
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
My first name is $map.firstName
My last name is $map.lastName