【问题标题】:Velocity using #parse where the file doesn't exist在文件不存在的情况下使用 #parse 的速度
【发布时间】:2010-11-10 14:39:45
【问题描述】:

我们使用 Velocity 为几个不同的环境模板化我们的配置文件,这个过程非常有效,但我有一个关于解析不存在的文件的快速问题。

我的问题是: 解析前如何检查文件是否存在?

因此,在示例中,文件 default.user.properties 可能合法地不存在,如果不存在,则文件的其余部分不会被解析。

#parse("./default.user.properties")

我知道一种解决方案是确保文件始终存在,但如果我不必这样做会很好。

提前致谢。

【问题讨论】:

    标签: velocity template-engine


    【解决方案1】:

    刚刚用弹簧和速度做到了这一点:

    我在获取事件处理程序的速度时遇到问题,最后在 servlet xml 文件中指定它:

    <bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
            <property name="resourceLoaderPath" value="WEB-INF/templates"/>
            <property name="velocityPropertiesMap">
                <map>
                    <entry key="eventhandler.include.class"><value>com.velocity.events.OptionalIncludeEventHandler</value></entry>
                </map>
            </property>
        </bean>
    

    它根本不接受我将它放在属性文件中 - 它会实例化类,但不会将其注册为事件侦听器。非常令人沮丧。

    这个类本身很简单,是对现有速度类“org.apache.velocity.app.event.implementIncludeNotFound”的公然抄袭。现有的速度实现检查文件是否存在,如果不存在,则返回可配置的替代方案(默认值:notfound.vm)。

    我的完全一样,只是如果文件不存在则返回 null,导致解析器跳过这个包含/解析指令:

    public class OptionalIncludeEventHandler implements IncludeEventHandler, RuntimeServicesAware {
    
        private RuntimeServices rs;
    
        @Override
        public void setRuntimeServices(RuntimeServices rs) {
            this.rs = rs;
        }
    
        @Override
        public String includeEvent(String includeResourcePath, String currentResourcePath, String directiveName) {
            return rs.getLoaderNameForResource(includeResourcePath) != null ? includeResourcePath : null;
        }
    
    }
    

    像魅力一样工作。

    希望有用。

    【讨论】:

      【解决方案2】:

      我使用的解决方案是创建一个用于检查模板是否存在的实用方法,即

      public synchronized boolean templateExists(String templateFilename) {
          Boolean templateExists = this.templateExistsCache.get(templateFilename);
          if (templateExists != null) {
              return templateExists;
          }
          String absoluteFilename = this.request.getSession().getServletContext().getRealPath(
                  "/WEB-INF/templates/" + templateFilename);
          File templateFile = new File(absoluteFilename);
          templateExists = templateFile.exists();
          this.templateExistsCache.put(templateFilename, templateExists);
          return templateExists;
      }
      
      private Map<String, Boolean> templateExistsCache = new HashMap<String, Boolean>();
      

      来自

      https://github.com/okohll/agileBase/blob/master/gtpb_server/src/com/gtwm/pb/model/manageData/ViewTools.java

      【讨论】:

        【解决方案3】:

        一种新型事件处理程序“IncludeEventHandler”。这允许开发人员定义一个类(实现 IncludeEventHandler),每次评估 #parse 或 #include 时都会调用该类。您的事件处理程序的目的是检查模板是否存在,如果不存在,则为调用代码设置错误标志。 尽管我自己没有测试过,但请查看文档以获取更多信息

        【讨论】:

        • 感谢您的回答,org.apache.velocity.app.event.IncludeEventHandler 看起来很有趣,虽然比我希望的要多一点努力,:) 当我们合并/模板化文件时它也很复杂全部来自 ant,所以不清楚我将如何插入 IncludeEventHandler 的新实现。但我会跟进并发布我的发现。
        猜你喜欢
        • 2012-07-21
        • 2010-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-04
        • 2017-12-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多