问题描述
我想在独立应用程序中使用 VelocityTool 的 GenericTools 进行一些标准格式设置.例如在我的 Velocity 模板中有这样的东西来使用 GenericTools 的 NumberTool 格式化程序:
I want to use VelocityTool's GenericTools for some standard formatting in a standalone app. e.g. have something like this in my Velocity template to use the GenericTools' NumberTool formatter:
Total: $numberTool.format("#0.00", $totalPnL)
如何将上面的$numberTool"与 GenericTool NumberTool 相关联.这是我的速度代码:
How do I associate the above "$numberTool" with the GenericTool NumberTool. Here's my Velocity code:
Velocity.init();
VelocityContext velocityContext = new VelocityContext();
Template template = Velocity.getTemplate("example.vm");
velocityContext.put("totalPnL", 100);
StringWriter sw = new StringWriter();
template.merge(velocityContext, sw);
现在我知道我可以这样做以使其正常工作:
Now I know I can do this to get it to work:
velocityContext.put("numberTool", new NumberTool());
但是这是我需要将所有 GenericTools 添加到我的应用程序的方式吗?手动一次一个(例如 DateTool 的另一行......等)?没有办法让所有 GenericTools 暴露给我的模板吗?我知道 VelocityTools 附带了一个tools.xml",它定义了 GenericTools.我可以将它添加到我的应用程序中以公开所有工具吗?如果是这样,如何?
But is that how I need to add all the GenericTools to my app? Manually and one at a time (e.g. another line for DateTool ... etc)? Isn't there a way to make all the GenericTools exposed to my template with out this? I know there's a "tools.xml" that comes with VelocityTools that has the GenericTools defined. Can I just add that to my app to expose all the tools? If so, how?
谢谢,大卫
推荐答案
http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/ToolManager.html
http://velocity.apache.org/tools/devel/standalone.html
默认工具配置已经提供了所有通用工具.如果你想配置这些工具,你可以创建一个配置.甚至可以自动加载配置或手动指定.
The default tool configuration provides all the generic tools already. Though you can create a config if you want to configure those tools. There's even auto loading for configurations, or manual specification.
ToolManager tm = new ToolManager();
tm.setVelocityEngine(yourVelocityEngine);
Context context = tm.createContext();
这篇关于在独立应用程序中使用 Velocity 的 GenericTools 的更好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!