Velocity Quick Start [5]-using object attributes and methods in templates

Source: Internet
Author: User
Source File 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>
* Fifth learning instance
* </P>
*
* <P> the passed parameter is an object, and the object operation in the template </P>
*
* Create on 2006-4-3 14:43:17
*
* @ Author Jonathan Q. Bo
* @ Version 1.0 valocity Study
*/
Public class fiveexample {
 
/**
* Test Method
*/
Public void test (){
Try {
/* Initialize the runtime engine and specify the template folder location for initialization */
Properties P = new properties ();
P. setproperty ("file. Resource. loader. Path", "VM ");
Velocity. INIT (P );

/* Create the context and put the data */
VelocityContext context = new VelocityContext ();
Context. put ("exampleObject", new ExampleObject ());

/* Output target of parsed data, subclass of java. io. Writer */
FileWriter w = new FileWriter ("gen/systthexamplegen. java ");
BufferedWriter bw = new BufferedWriter (w );


/* Parse */
// Velocity. mergeTemplate ("secondtemplate. vm", context, w );
Velocity. mergeTemplate ("effecthtemplate. vm", "gb2312", context, bw );

Bw. flush ();
Bw. close ();
System. Out. println ("#... successful! ");
} Catch (resourcenotfoundexception E1 ){
System. Out. println ("# The source file does not exist! ");
E1.printstacktrace ();
} Catch (parseerrorexception E2 ){
System. Out. println ("# file parsing error! ");
E2.printstacktrace ();
} Catch (methodinvocationexception E3 ){
System. Out. println ("# method call exception! ");
E3.printstacktrace ();
} Catch (exception E4 ){
System. Out. println ("# other errors! ");
E4.printstacktrace ();
}
}
 public static void main(String[] args) {
  FiveExample example = new FiveExample();
  example.test();
 }
}
 
Source File ExampleObject. java
package nc.jonathan.velocity;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* <P>
* Objects used in the template
* </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;
 }
}
Template File
/**********************************/
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())
Output file
/**********************************/
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.