Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

velocity templates

843840Aug 22 2008 — edited Aug 22 2008
hi all!!!

i'm having trouble with tomcat and velocity templates

i found on the internet this code. but no matter what i do i cannot get this to work. it never displays the correct values.
import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

public class VMDemo {

  public static void main(String[] args) throws Exception {
    Velocity.init();
    Template t = Velocity.getTemplate("./src/VMDemo.vm");

    VelocityContext ctx = new VelocityContext();

    ctx.put("aDate",new Date());
    Writer writer = new StringWriter();
    t.merge(ctx, writer);

    System.out.println(writer);
  }
}

-------------------------------------------------------------------------------------
//File: VMDemo.vm

Day: $aDate.getDate()
Month: $aDate.getMonth()
Year: $aDate.getYear()
the only thing i can think of is that i do not put the class or .jar archive in the correct spot and it is not seen by tomcat.
i place either in WEB-INF/lib if i create a .jar archive or use only the .class file no change. in the logs i can only see that '$aDate has no value' line
and that's about it.

i wrote my own code too (along the example above)
import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

public class Viktor {
        private String puppet;
        public Viktor(String puppet){
                this.puppet = puppet;
        }
        public String getResult(){
                return puppet;
        }
        public static void main(String[] args) throws Exception {
              Velocity.init();
              Template t = Velocity.getTemplate("/opt/ConfigEngine/CSCOcnsie/Templates/device1.vm");
              VelocityContext ctx = new VelocityContext();

              //ctx.put("aDate",new Date());
              ctx.put("viktor",new Viktor("puppet"));
              Writer writer = new StringWriter();
              t.merge(ctx, writer);

                System.out.println(writer);
        }
}

---------------------------------------------------------------------------------
device1.vm

#set($viktor = $viktor.getResult())
viktor is $viktor
and again no value what so ever!!! ):

basically what i'm trying to do is to get custom java code talk to templates within Cisco Configuration Engine. if anybody has any experience with it, please, let me know.
any links, tuts anything will be good!

thanks.
v..v
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 19 2008
Added on Aug 22 2008
1 comment
299 views