2008年3月13日

Apache Velocity Project

Apache Velocity Project

import java.io.StringWriter;
import java.sql.Date;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

public class Main {
public static void main(String[] args) throws Exception {
// add ClasspathResourceLoader
Velocity.setProperty("class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader." +
"ClasspathResourceLoader");
Velocity.setProperty("class.resource.loader.cache", "true");
Velocity.setProperty("resource.loader", "class, file");
Velocity.init();

Template template = Velocity.getTemplate("model.vm");
VelocityContext context = new VelocityContext();


context.put("name", "Solnone");
context.put("age", new Integer(33));
Date birthday = java.sql.Date.valueOf("1971-01-01");
context.put("birthday", birthday);

StringWriter sw = new StringWriter();
template.merge(context, sw);
System.out.println(sw);
}
}
/* In Classpath /model.vm

Name: ${name}
Age: ${age}
birthday: ${birthday}
*/

/* Out put

Name: Solnone
Age: 33
birthday: 1971-01-01
*/

沒有留言:

網誌存檔