文件不存在时使用 #parse 的速度
我们使用 Velocity 为几个不同的环境模板化我们的配置文件,这个过程运行得非常好,但我有一个关于解析不存在的文件的快速问题。
我的问题是: 在解析文件之前如何检查文件是否存在?
因此,在示例中,文件 default.user.properties 可能合法地不存在,如果不存在,则不会解析文件的其余部分。
#parse("./default.user.properties")
我知道一种解决方案是确保文件始终存在,但如果我不需要这样做那就太好了。
提前致谢。
we use Velocity to template our configuration files for several different environments and this process works really well but I have a quick question relating parsing a file that doesn't exist.
My question is:
How do you check if a file exists before parsing it?
So in the example the file default.user.properties might legitimately not exist and if it doesn't the rest of the file doesn't get parsed.
#parse("./default.user.properties")
I know one solution is to make sure the file is always there but it would be nice if I didn't have to.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是用弹簧和速度做到了这一点:
我在获取速度来拾取事件处理程序时遇到问题,最后在 servlet xml 文件中指定它:
它根本不接受我将其放入属性文件中 - 它会实例化该类,但不将其注册为事件侦听器。 非常令人沮丧。
该类本身很简单,是对现有速度类“org.apache.velocity.app.event.implementIncludeNotFound”的明显抄袭。 现有的速度实现检查文件是否存在,如果不存在,则返回可配置的替代方案(默认值:notfound.vm)。
我的完全一样,只是如果文件不存在则返回 null,导致解析器跳过此包含/解析指令:
就像一个魅力一样。
希望它有用。
Just did this with spring and velocity:
I had problems getting velocity to pick up my event handler, in the end specifying it in the servlet xml file:
It simply would not accept me putting it in the properties file - it would instantiate the class, but not register it as an event listener. very frustrating.
The class itself is simple, a quite blatant rip off from the existing velocity class "org.apache.velocity.app.event.implementIncludeNotFound". The existing velocity implementation checks for the existance of the file, and if not present returns a configurable alternative (default: notfound.vm).
Mine is exactly the same except it returns null if the file does not exist, causing the parser to skip this include / parse directive:
Works like a charm.
Hope it's useful.
一种新类型的事件处理程序“IncludeEventHandler”。 这允许您(开发人员)定义一个类(实现 IncludeEventHandler),每次评估 #parse 或 #include 时都会调用该类。 事件处理程序的目的是检查模板是否存在,如果不存在,则为调用代码设置错误标志。
尽管我自己没有测试过,但请查看文档以获取更多信息
A new type of event handler "IncludeEventHandler". This allows you, the developer, to define a class (implementing IncludeEventHandler) which will be called every time a #parse or #include is evaluated. The purpose of your event handler would be to check if the template exists and set an error flag for the calling code if not.
Check the documentation for more information though I haven't tested it myself
我使用的解决方案是创建一个实用方法来检查模板是否存在,即
来自
https://github.com/okohll/agileBase/blob/master/gtpb_server/src/com/gtwm/pb/model/manageData/ViewTools.java
The solution I used is to create a utility method for checking template existence, i.e.
from
https://github.com/okohll/agileBase/blob/master/gtpb_server/src/com/gtwm/pb/model/manageData/ViewTools.java