The Field class needs to be a public class for Velocity to be able to
access its properties.

  Claude


On lun., 2010-03-01 at 21:09 -0500, czy11421 wrote:
> Hi, ALL,
> 
> I try to write a class file generation based on Velocity, very simple, 
> but it doesn't work. The problem is, it will not output field type and 
> name.
> 
> what reason, need import something ?
> 
> Thanks.
> 
> Edward
> 
> 
> [code]
> package com;
> 
> import java.io.BufferedWriter;
> import java.io.OutputStreamWriter;
> import java.util.ArrayList;
> import java.util.Properties;
> 
> import org.apache.velocity.Template;
> import org.apache.velocity.VelocityContext;
> import org.apache.velocity.app.Velocity;
> import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
> 
> public class Test {
>     
>     public static void main(String[] args) {
>         VelocityContext context = new VelocityContext();
>         context.put("fldList", getFld());
>         context.put("CName", "Employee");
>         context.put("SName", "employee");
> 
>         Template template = null;
> 
>         try {
>             Properties p = new Properties();
> 
>             p.setProperty("file.resource.loader.class", 
> ClasspathResourceLoader.class.getName());
>             Velocity.init(p);
>             
>             template = Velocity.getTemplate("example.vm");
> 
>             BufferedWriter writer = new BufferedWriter(new 
> OutputStreamWriter(System.out));
> 
>             if (template != null){
>                 template.merge(context, writer);
>             }                
> 
>             writer.flush();
>             writer.close();
>         } catch (Exception e) {
>             e.printStackTrace();
>             System.out.println(e.getMessage());
>         }
> 
>     }
> 
>     public static ArrayList<Field> getFld() {
>         ArrayList<Field> list = new ArrayList<Field>();
>         list.add(new Field("String", "name"));    
>         return list;
>     }
> }
> 
>  class Field {
>     public String name;
>     public String type;
>     
>     public Field(){    }
>     
>     public Field(String type, String name){
>         this.type = type;
>         this.name = name;            
>     }
>     
>     public String getName() {
>         return name;
>     }
> 
>     public void setName(String name) {
>         this.name = name;
>     }
> 
>     public String getType() {
>         return type;
>     }
> 
>     public void setType(String type) {
>         this.type = type;
>     }
> 
>     @Override
>     public String toString() {        
>         return this.name+"--"+this.type;
>     }
> 
> 
> }
> 
> [/code]
> vm file
> [code]
> public class $CName{
>     #foreach( $field in $fldList )
>         private ${field.type}  ${field.name};        
>     #end
> }
> 
> [/code]
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to