You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by czy11421 <cz...@gmail.com> on 2010/03/02 03:09:06 UTC
why this code doesn't work ?
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: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: why this code doesn't work ?
Posted by Claude Brisson <cl...@renegat.net>.
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: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org