Better way to use Velocity's GenericTools in a Standalone app?

雨燕双飞 提交于 2019-12-04 19:25:54

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

it is at least the way I do it too. I'll put for example

context.put("esc", new EscapeTool());

and in the template I simply use then

${esc.h}

to write a "#" in the code so that Velocity does not parse it as "velocity-script".

I think those helper tools are rather utils and only cover some basic signs. They are not intend to be a standard, you rather can include them on-demand.

I've build for example an abstract class that loads the context of velocity and puts the EscapeTool into the context all the time so that I do not have to add it everywhere.

Good luck with your project

Sebastian

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!