Java 无法在Apache velocity中显示对象的实例变量
我试图使用ApacheVelocity显示一些信息,但是它似乎不起作用 下面是代码片段Java 无法在Apache velocity中显示对象的实例变量,java,velocity,Java,Velocity,我试图使用ApacheVelocity显示一些信息,但是它似乎不起作用 下面是代码片段 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.vel
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;
}
}
然而,结果是:
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()
我不明白为什么阶级车辆的变量没有得到解决。在黑暗中拍摄,但是你试过把你的阶级公之于众吗?哇,成功了。你能指出文档中提到的类必须是公共的吗?不知道为什么$vehicle.hashCode()可以工作,即使我跳过了hashCode方法。另外,请回复,以便我能接受。谢谢
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()