Java2014.08.19 14:01




자바 버젼 Velocity 샘플입니다.

jar는 두가지를 참조했습니다. velocity-1.7.jar, velocity-1.7-dep.jar

 

import java.io.OutputStreamWriter;
import java.util.ArrayList;

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 VelocityExMain {

	public static void main(String[] args) {
		Example("templateHTML.vm");
	}

	public static void Example(String templateFile) {
		try {

			try {
				Velocity.init();
			} catch (Exception e) {
				System.out.println("Problem initializing Velocity : " + e);
				return;
			}

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

			Template template = null;

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

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

			FileWriter writer = new FileWriter("newHTML.txt");

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

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

	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;
	}
}
#foreach( $name in $list )
    $name is great!
#end

#set( $condition = true)

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

$newPara is good man.
신고




Posted by 금붕어70