I have the following code that prints out something like a database using a velocity engine and the output is printed to a console. Though what I'm asked is to generate HTML code using a Velocity template.

The Velocity Test Class

import java.io.StringWriter;

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

public class VelocityTest {
        public static void main( String[] args ) throws Exception {
                VelocityEngine ve = new VelocityEngine();
                ve.init();
                Template t = ve.getTemplate( "hello.vm" );
                VelocityContext context = new VelocityContext();
                StringWriter writer = new StringWriter();
                CarInfo[] car=new CarInfo[10];
                car[0] = new CarInfo("BMW", "X","6","red","3.0","200","30","05","2010","manual","diesel","premium");
                car[1] = new CarInfo("Seat", "Ibiza", "2","not specified","1.0", "60", "09", "09", "1998", "manual", "petroleum","95");
                car[2] = new CarInfo("Skoda","Octavia","3","black","1.6","120","22","08","2008","automatic","diesel", "not specified");
                car[3] = new CarInfo("Citroen", "c","4", "yellow", "1.2", "100","14", "06", "2004","not specified", "diesel", "light");
                car[4] = new CarInfo("VW", "golf", "2", "blue", "1.6", "120", "12", "07", "2002", "semi-automatic", "not specified","not specified");
                car[5] = new CarInfo("BMW", "x","1", "white", "1.8", "180", "23", "05", "2005","not specified","not specified","not specified");
                car[6] = new CarInfo("Citroen", "c","2", "green", "1.0", "90", "12", "12", "2012", "semi-automatic", "petroleum","not specified");
                car[7] = new CarInfo("Fiat","panda", "3", "purple","1.6", "100", "31", "10", "1999", "semi-automatic","not specified","not specified");
                car[8] = new CarInfo("BMW", "M","5", "beige", "2.8", "260", "22", "12", "1993","not specified","petroleum","not specified");
                car[9] = new CarInfo("VW", "Transporter", "1", "not specified", "2.5", "200","29", "09","1998", "not specified","diesel", "light");
                for(int i=0; i<10; i++){
                    context.put("car", car[i]);
                    t.merge( context, writer );
               } 

                System.out.println( writer.toString() );



        }
}

CarInfo Class

public class CarInfo {
    private String maker, model, series, coloring, capacity, power, day, month, year, transmission, name, quality;

    public CarInfo (String maker, String model, String series, String coloring, String capacity, String power, String day, String month, String year, String transmission, String name, String quality) {
            this.maker = maker;
            this.model = model;
            this.series= series;
            this.coloring = coloring;
            this.capacity = capacity;
            this.power = power;
            this.day = day;
            this.month = month;
            this.year = year;
            this.transmission = transmission;
            this.name = name;
            this.quality = quality;
    }

    public String getMaker() {
            return this.maker;
    }


    public String getModel() {
            return this.model;
    }

    public String getSeries(){
            return this.series;
    }

    public String getColoring(){
            return this.coloring;
    }

    public String getCapacity(){
        return this.capacity;
    }

    public String getPower(){
        return this.power;
    }

    public String getDay(){
        return this.day;
    }

    public String getMonth(){
        return this.month;
    }

    public String getYear(){
        return this.year;
    }

    public String getTransmission(){
        return this.transmission;
    }

    public String getName(){
        return this.name;
    }

    public String getQuality(){
        return this.quality;
    }
}

And finally the Template, hello.vm file

This car is made by $car.maker, model $car.model, series $car.series, color $car.coloring.It has an engine of capacity $car.capacity and power of $car.power. It was fabricated at $car.day.$car.month.$car.year . The transmission is $car.transmission. It runs on $car.name , quality $car.quality .

Question is, how can I generate the HTML code? I tried writing into the .vm HTML code but it's not like it will change its extension or whatever. From what I've seen there's a way of doing it with a servlet but that's as far as my knowledge about this goes.

Not sure I completely understand your question. Velocity is just a templating language which is concerned with rendering data based on your existing view/template. If you need HTML output, you'll have a create a template file which follow the HTML specification. Something like:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Cars!</title>
</head>
<body>
    #foreach( $car in $cars )
        <p>Car name: $car.name -- Car color: $car.color</p><br/>
    #end
</body>
</html>

And run it using:

public class VelocityTest {

    public static List<AutoMobile> getCars() {
        ArrayList<AutoMobile> cars = new ArrayList<>();
        cars.add(new AutoMobile("Skoda Octavia", "Red"));
        cars.add(new AutoMobile("Fiat Panda", "Blue"));
        return cars;
    }

    public static void main(String[] args) {
        VelocityEngine ve = new VelocityEngine();
        ve.init();
        Template t = ve.getTemplate("cars.vm");
        VelocityContext context = new VelocityContext();
        context.put("cars", getCars());
        StringWriter writer = new StringWriter();
        t.merge(context, writer);
        System.out.println(writer.toString());
    }

}

public class AutoMobile {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public void setColor(String color) {
        this.color = color;
    }
    private String color;
    public AutoMobile(String name, String color) {
        this.name = name;
        this.color = color;
    }
    public String getName() {
        return name;
    }
    public String getColor() {
        return color;
    }
}

Just write the return value of writer.toString() to a file with HTML extension; opening up this file in the browser now should render the HTML view.

Also, this article might help you out with Velocity in general.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.