【问题标题】:velocity template format速度模板格式
【发布时间】:2023-03-03 15:37:01
【问题描述】:

我有这样的速度模板:

+-----------------------------------------------------------+-------------+-------------+-------------+-------------+
| $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());

【问题讨论】:

  • 我真的不明白你在这里想要什么......有什么区别?只是更多的空间?这通常在模板中进行控制。
  • 是的,我只想为结果自动添加空间。当我输入很多参数时它很有用:D

标签: java velocity template-engine


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以将值转换为字符串并填充它,或者您可以简单地在模板中添加更多空间,我相信速度会保持您定义的空白。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多