velocity 的简单的使用

public static String mergeTemplateIntoString(String template, Map<String, Object> model) throws Exception {
    // 填充后返回值
    StringBuffer buff = null;
    // 初始化并取得Velocity引擎   
    VelocityEngine engine = new VelocityEngine();  
    // 取得velocity的上下文context   
    VelocityContext context = new VelocityContext(model);   
    StringWriter writer = new StringWriter();
    engine.evaluate(context, writer, "", template); 
    buff = writer.getBuffer();
    try {
        writer.close();
    } catch (IOException e) {
    }
    return buff.toString();
}


public static void main(String[] args) throws Exception {
    String template = "${owner}:您的${type} : ${bill} 在  ${date} 日已支付成功"; 
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("owner", "nassir");   
    model.put("bill", "201203221000029763");   
    model.put("type", "订单");   
    model.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    System.out.println(mergeTemplateIntoString(template, model));
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Velocity进行代码生成的过程通常涉及以下几个步骤: 1. 准备模板文件:首先,你需要创建Velocity模板文件,它是用来描述生成代码的结构和内容的。模板文件通常使用`.vm`作为扩展名。 2. 配置Velocity引擎:接下来,你需要配置Velocity引擎,以便正确加载和解析模板文件。这涉及设置Velocity的属性,如模板路径、编码等。 3. 创建数据模型:在生成代码之前,你需要准备好所需的数据。可以创建一个Java对象作为数据模型,将需要的数据存储在该对象中。 4. 合并模板和数据:通过将数据模型与模板进行合并,可以生成最终的代码。这可以通过调用Velocity引擎的`mergeTemplate`方法来实现。你需要提供模板文件的路径和数据模型作为参数。 5. 输出生成的代码:最后,将生成的代码输出到所需的位置,如文件、控制台等。你可以使用Java的IO操作来实现。 以下是一个简单的示例代码,演示了使用Velocity生成Java类的过程: ```java import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; public class CodeGenerator { public static void main(String[] args) { // 配置Velocity引擎 Properties properties = new Properties(); properties.setProperty("resource.loader", "file"); properties.setProperty("file.resource.loader.path", "path/to/templates"); VelocityEngine velocityEngine = new VelocityEngine(); velocityEngine.init(properties); // 创建数据模型 VelocityContext context = new VelocityContext(); context.put("className", "MyClass"); context.put("packageName", "com.example"); // 合并模板和数据 Template template = velocityEngine.getTemplate("classTemplate.vm"); StringWriter writer = new StringWriter(); template.merge(context, writer); // 输出生成的代码到文件 try { File outputFile = new File("path/to/output/MyClass.java"); FileWriter fileWriter = new FileWriter(outputFile); fileWriter.write(writer.toString()); fileWriter.close(); System.out.println("代码生成完成!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上述示例中,你需要将模板文件放置在指定的路径(`path/to/templates`),并创建一个名为`classTemplate.vm`的模板文件。模板文件中可以使用Velocity的语法来定义代码结构和使用数据模型中的值。 当运行代码时,Velocity引擎将加载模板文件,合并数据模型,并将生成的代码输出到指定的位置(`path/to/output/MyClass.java`)。 这只是一个简单的示例,你可以根据需要进行更复杂的代码生成。Velocity提供了丰富的语法和功能,可以根据你的需求来定制生成过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值