模板技术

velocity

模板技术

velocity 默认模板的后缀vm
freemarker 默认模板的后缀ftl
注意:后缀可以改
Velocity可以用来做什么

动态页面静态化:xxx.html
在后台准备数据,在前台准备模板,通过IO把数据与模板合- 并,真正的生成一个html页面出来
用作发送邮件、短信模板
代码生成器

Velocity入门

@Test
public void testVelocity01() throws Exception {
//创建模板应用上下文
VelocityContext context = new VelocityContext();
context.put(“msg”, “小张是个好同志”);
//拿到相应的模板(需要设置好编码)
Template template = Velocity.getTemplate(“temptest/hello.html”,“UTF-8”);
//准备输出流
StringWriter writer = new StringWriter();
template.merge(context, writer);
System.out.println(writer);
}

 @Test
public void testVelocity02() throws Exception {
    //创建模板应用上下文
    VelocityContext context = new VelocityContext();
    context.put("msg", "小张是个好同志");
    //拿到相应的模板(需要设置好编码)
    Template template = Velocity.getTemplate("temptest/hello.html","UTF-8");
    //准备输出流
    File file = new File("temptest/helloNew.html");
    FileWriter writer = new FileWriter(file);
    template.merge(context, writer);
    writer.close();
}

猜你喜欢

转载自blog.csdn.net/Secutiry_/article/details/88940969