Velocity Quick Start [5] - 在模板中使用对象属性、方法

来源:互联网 发布:少儿英语培训 知乎 编辑:程序博客网 时间:2024/06/01 10:21
源文件 FiveExample.java
package nc.jonathan.velocity;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

/**
 *<p>
 * 第五个学习实例
 *</p>
 *
 *<p> 传递的参数为对象,模板中的对象操作 </p>
 *
 * Create on 2006-4-3 14:43:17
 *
 * @author Jonathan Q. Bo
 * @version 1.0 valocity study
 */
public class FiveExample {
 
 /**
  * 测试方法
  */
 public void test(){
  try {
   /* 初始化运行时引擎, 指定模版文件夹位置进行初始化 */
   Properties p = new Properties();
      p.setProperty("file.resource.loader.path", "vm");
   Velocity.init(p);
   
   /* 建立context, 并放入数据*/
   VelocityContext context = new VelocityContext();
   context.put("exampleObject",new ExampleObject());
   
   /* 解析后数据的输出目标,java.io.Writer的子类 */
   FileWriter w = new FileWriter("gen/FifthExampleGen.java");
   BufferedWriter bw = new BufferedWriter(w);
   
   
   /* 进行解析 */
   //Velocity.mergeTemplate("secondtemplate.vm",context,w);
   Velocity.mergeTemplate("fifthtemplate.vm","gb2312",context,bw);
   
   bw.flush();
   bw.close();
   System.out.println("## ... successful!");
  } catch (ResourceNotFoundException e1) {
   System.out.println("## 源文件不存在!");
   e1.printStackTrace();
  } catch (ParseErrorException e2) {
   System.out.println("## 解析文件错误!");
   e2.printStackTrace();
  } catch (MethodInvocationException e3) {
   System.out.println("## 方法调用异常!");
   e3.printStackTrace();
  } catch (Exception e4){
   System.out.println("## 其他错误!");
   e4.printStackTrace();
  }
 }
 public static void main(String[] args) {
  FiveExample example = new FiveExample();
  example.test();
 }
}
 
源文件 ExampleObject.java
package nc.jonathan.velocity;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *<p>
 *  模板中使用的对象
 *</p>
 *
 * Create on 2006-4-3 18:55:33
 *
 * @author Jonathan Q. Bo
 * @version 1.0 valocity study
 */
public class ExampleObject{ 
 private String name;
 private List friends;
 
 public ExampleObject(){
  this.name = "jonathan";
  friends = new ArrayList();
  for(int i = 0; i < 8; i++){
   friends.add(" friend" + i);   
  }
 }
 
 public String getAllFriends(){
  String all = name + "'s friends : ";
  for(Iterator itt = friends.iterator(); itt.hasNext(); ){
   all += "," + (String)itt.next();
  }
  return all;
 }
 
 public String rename(String newName){
  this.setName(newName);
  return newName;
 }
 
 public String getNewName(){
  String[] names = {"Jacy","Tom","Cat","Random"};
  int random  = (int)(Math.random()*10%3);
  return names[random];
 }
 
 public List getFriends() {
  return friends;
 }
 public void setFriends(List friends) {
  this.friends = friends;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
}
模板文件
/**********************************/
test object property : ${exampleObject.name}
test object property : $exampleObject.name
test object property: $exampleObject.getName()
/***********************************/
test object method : ${exampleObject.getAllFriends()}
test object method : $exampleObject.getAllFriends()
/***********************************/
test object method : $exampleObject.rename($exampleObject.getNewName())
输出文件
/**********************************/
test object property : jonathan
test object property : jonathan
test object property: jonathan
/***********************************/
test object method : jonathan's friends : , friend0, friend1, friend2, friend3, friend4, friend5, friend6, friend7
test object method : jonathan's friends : , friend0, friend1, friend2, friend3, friend4, friend5, friend6, friend7
/***********************************/
test object method : Jacy
原创粉丝点击