速度模板格式

时间:2011-10-05 15:52:13

标签: java velocity template-engine

我有像这样的速度模板:

+-----------------------------------------------------------+-------------+-------------+-------------+-------------+
| $totalPel  Pelanggan                                      |    $totalBk |   $totalAdm |   $totalTag | $totalTotal |
+-----------------------------------------------------------+-------------+-------------+-------------+-------------+

当我把$ totalPel - > 100,结果是:

+-----------------------------------------------------------+-------------+-------------+-------------+-------------+
| 100  Pelanggan                                      |    $totalBk |   $totalAdm |   $totalTag | $totalTotal |
+-----------------------------------------------------------+-------------+-------------+-------------+-------------+

其实我想得到这样的结果:

+-----------------------------------------------------------+-------------+-------------+-------------+-------------+
| 100        Pelanggan                                      |    $totalBk |   $totalAdm |   $totalTag | $totalTotal |
+-----------------------------------------------------------+-------------+-------------+-------------+-------------+

任何人都可以告诉我如何制作它?

我使用这个java代码:

Velocity.init();

VelocityContext context = new VelocityContext();
context.put("totalPel", 100);

Template template = Velocity.getTemplate("LaporanTagihan.txt");

StringWriter writer = new StringWriter();

template.merge(context, writer);

System.out.println(writer.toString());

2 个答案:

答案 0 :(得分:4)

在将字符串添加到模板之前填充字符串。有关如何执行此操作的建议,请参阅How can I pad a String in Java?

答案 1 :(得分:2)

你可以将值转换为String并填充它,或者你只需​​在模板中添加更多空间,我相信velocity可以保持你定义的空格。