我试图使用Apache速度显示一些信息,但它似乎不起作用。
Follwing是代码段。
package velocitydemo;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
class Vehicle {
private String vname;
private Wheel wheel;
private String type;
/**
* @return the vname
*/
public String getVname() {
return vname;
}
/**
* @param vname the vname to set
*/
public void setVname(String vname) {
this.vname = vname;
}
/**
* @return the wheel
*/
public Wheel getWheel() {
return wheel;
}
/**
* @param wheel the wheel to set
*/
public void setWheel(Wheel wheel) {
this.wheel = wheel;
}
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the model
*/
public int getModel() {
return model;
}
/**
* @param model the model to set
*/
public void setModel(int model) {
this.model = model;
}
private int model;
public Vehicle(String name, Wheel wheel, String type, int model) {
super();
this.vname = name;
this.wheel = wheel;
this.type = type;
this.model = model;
}
}
class Wheel {
private String type;
private String name;
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
public Wheel(String type, String name) {
super();
this.type = type;
this.name = name;
}
}
public class HelloWorld {
private static List<String> list = new ArrayList<String>();
public static void main(String[] args) throws Exception {
Vehicle vh = constructVehicle("Model S", "Vinyl", "JK", "Electric", 2014);
VelocityEngine ve = new VelocityEngine();
ve.init();
/* next, get the Template */
Template t = ve.getTemplate("helloworld.vm");
/* create a context and add data */
VelocityContext context = new VelocityContext();
context.put("name", "World");
context.put("name2", "foobar");
context.put("name3", "flash");
context.put("vehicle", vh);
StringWriter writer = new StringWriter();
t.merge(context, writer);
System.out.println(writer.toString());
}
public static Vehicle constructVehicle(String vehicleName, String wheelType, String wheelName, String vhType, int model) {
Wheel wl = new Wheel(wheelType, wheelName);
Vehicle vh = new Vehicle(vehicleName, wl, vhType, model);
return vh;
}
}
速度模板是helloworld.vm,其中包含以下信息
Hello $name! Welcome to Velocity!
Hello $name2! Welcome to Velocity!
Hello $name3! Welcome to Velocity!
Car is $vehicle.vname
Car is $vehicle.getVname()
Car is $vehicle.hashCode() and model is $vehicle.getModel()
然而输出是:
Hello World! Welcome to Velocity!
Hello foobar! Welcome to Velocity!
Hello flash! Welcome to Velocity!
Car is $vehicle.vname
Car is $vehicle.getVname()
Car is 14872264 and model is $vehicle.getModel()
我不明白为什么类车辆的变量没有得到解决。