public static void main(String[] args) throws IOException {
VelocityEngine velocity = new VelocityEngine();
Properties properties = new Properties();
// 指定字符集
properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
String basePath = "templates";//路径为.vm文件所在的文件夹名称!!!此处文件名为templates
// 设置模板的路径
properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);
// 初始化velocity 让设置的路径生效
velocity.init(properties);
// 获取模版
Template template = velocity.getTemplate("first.vm");
// 创建context
VelocityContext context = new VelocityContext();
// 添加数据
context.put("name", new String("Velocity"));
// Merge 模版和context
StringWriter writer = new StringWriter();
template.merge(context, writer);
//渲染到控制台
System.out.println(writer.toString());
writer.close();
}
velocity模板引擎,读取不到模板请看如下格式即可!!
最新推荐文章于 2024-05-11 10:24:57 发布