Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Open Source Projects
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Other Open Source Projects
Velocity question
Edward Chen
Ranch Hand
Posts: 798
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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.
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; } }
vm file
public class $CName{ #foreach( $field in $fldList ) private ${field.type} ${field.name}; #end }
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Help executing velocity example
Velocity Error
Freemarker question
Velocity Templates with Servlets
apache velocity #foreach loop
More...