Velocity 模板自动重新加载不起作用

发布于 2024-12-02 18:01:34 字数 1356 浏览 2 评论 0原文

我想在速度模板发生变化时重新加载它。为此,我设置了以下内容,但是当我手动更改 META-INF/template/ 内的 .vm 文件时,重新加载不起作用。

velocimacro.library.autoreload = true
[spring|file|class].resource.loader.cache = false

有什么想法吗?这是我的velocityEngine bean

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="resourceLoaderPath" value="classpath:META-INF/template/" />
    <property name="preferFileSystemAccess" value="false" />
    <property name="velocityProperties">
        <props>
            <prop key="spring.resource.loader.cache">false</prop>
            <prop key="file.resource.loader.cache">false</prop>
            <prop key="class.resource.loader.cache">false</prop>
            <prop key="velocimacro.library.autoreload">true</prop>

            <prop key="resource.loader">spring</prop>
            <prop key="directive.foreach.counter.name">counter</prop>
            <prop key="directive.foreach.counter.initial.value">0</prop>
            <prop key="spring.resource.loader.class">
                org.springframework.ui.velocity.SpringResourceLoader
            </prop>
        </props>
    </property>
</bean>

I want to reload velocity template as it's changed. For this I've set the followings, but reload doesn't work when I manually change a .vm file inside META-INF/template/ .

velocimacro.library.autoreload = true
[spring|file|class].resource.loader.cache = false

Any idea? Here is my velocityEngine bean

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="resourceLoaderPath" value="classpath:META-INF/template/" />
    <property name="preferFileSystemAccess" value="false" />
    <property name="velocityProperties">
        <props>
            <prop key="spring.resource.loader.cache">false</prop>
            <prop key="file.resource.loader.cache">false</prop>
            <prop key="class.resource.loader.cache">false</prop>
            <prop key="velocimacro.library.autoreload">true</prop>

            <prop key="resource.loader">spring</prop>
            <prop key="directive.foreach.counter.name">counter</prop>
            <prop key="directive.foreach.counter.initial.value">0</prop>
            <prop key="spring.resource.loader.class">
                org.springframework.ui.velocity.SpringResourceLoader
            </prop>
        </props>
    </property>
</bean>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬花如无物 2024-12-09 18:01:34

作为setResourceLoaderPath doc 说:

请注意,在任何情况下都会启用资源缓存。使用文件资源加载器,将在访问时检查最后修改的时间戳以检测更改。 使用SpringResourceLoader,资源将被永久缓存(例如类路径资源)。

...

要强制使用 SpringResourceLoader,即在任何情况下都不将路径解析为文件系统资源,请关闭“preferFileSystemAccess”标志。有关详细信息,请参阅后者的 javadoc。

然后对于setPreferFileSystemAccess

设置是否优先访问文件系统来加载模板。 文件系统访问支持模板更改的热检测。

如果启用此功能,VelocityEngineFactory 将尝试将指定的“resourceLoaderPath”解析为文件系统资源(这也适用于扩展类路径资源和 ServletContext 资源)。

默认为“true”。 关闭此功能以始终通过 SpringResourceLoader 加载(即作为流,无需热检测模板更改),如果某些模板驻留在扩展类目录中,而其他模板驻留在 jar 文件中,则可能需要这样做。

看来有一些因素导致了这个问题。使用 SpringResourceLoaderclasspath: 伪协议使得 Spring 无限缓存模板。最重要的是,preferFileSystemAccess 被禁用,这确保模板永远不会通过文件系统访问。

As setResourceLoaderPath doc says:

Note that resource caching will be enabled in any case. With the file resource loader, the last-modified timestamp will be checked on access to detect changes. With SpringResourceLoader, the resource will be cached forever (for example for class path resources).

...

To enforce the use of SpringResourceLoader, i.e. to not resolve a path as file system resource in any case, turn off the "preferFileSystemAccess" flag. See the latter's javadoc for details.

Then for setPreferFileSystemAccess

Set whether to prefer file system access for template loading. File system access enables hot detection of template changes.

If this is enabled, VelocityEngineFactory will try to resolve the specified "resourceLoaderPath" as file system resource (which will work for expanded class path resources and ServletContext resources too).

Default is "true". Turn this off to always load via SpringResourceLoader (i.e. as stream, without hot detection of template changes), which might be necessary if some of your templates reside in an expanded classes directory while others reside in jar files.

So looks like there are a few things that contribute to the problem. Using SpringResourceLoader with classpath: pseudo-protocol makes Spring cache the template infinitely. On top of that preferFileSystemAccess is disabled which makes sure that template is never accessed through the file system.

儭儭莪哋寶赑 2024-12-09 18:01:34

添加此属性为我解决了这个问题:

velocimacro.permissions.allow.inline.to.replace.global=true

使用下面的属性时我遇到了同样的问题,但添加上面的属性使我能够自动重新加载工作:

velocimacro.library.autoreload=true
file.resource.loader.cache=false

请参阅 如何在不重新启动速度的情况下编辑 velocimacro?

Adding this property resolved this issue for me:

velocimacro.permissions.allow.inline.to.replace.global=true

I had the same problem when using the properties below but adding the above property enabled me to get auto-reload to work:

velocimacro.library.autoreload=true
file.resource.loader.cache=false

Please see How to edit a velocimacro without restarting velocity?

陈甜 2024-12-09 18:01:34

好的,我的velocityEngine bean设置如下:(

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/templates/"/>
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">utf-8</prop>
             </props>
        </property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="cache" value="false"/>
        <property name="prefix" value=""/>
        <property name="suffix" value=".xhtml"/>
        <property name="contentType" value="text/html; charset=UTF-8" />
        <property name="exposeSpringMacroHelpers" value="true"/>
</bean>

假设你使用Spring框架,顺便说一句)
我对缓存没有任何问题。但我使用配置器 bean 而不是 beanfactory。尝试看看这个。

还有一件事,这很琐碎,但仍然......,修改模板后你真的重新部署你的项目吗?

Ok, i have velocityEngine bean set like this:

<bean id="velocityConfig" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
        <property name="resourceLoaderPath" value="/templates/"/>
        <property name="velocityProperties">
            <props>
                <prop key="input.encoding">utf-8</prop>
             </props>
        </property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
        <property name="cache" value="false"/>
        <property name="prefix" value=""/>
        <property name="suffix" value=".xhtml"/>
        <property name="contentType" value="text/html; charset=UTF-8" />
        <property name="exposeSpringMacroHelpers" value="true"/>
</bean>

(Assuming you use Spring framework, by the way)
and I have no problems with caching whatsoever. But I use the configurer bean instead of beanfactory. Try look into this.

And there's the thing, it's trivial, but still..., do you actually redeploy your project after modifying the template?

我过去也遇到过同样的问题,所以我写了这篇博客文章: Spring-mvc + Velocity + DCEVM

主要思想是至少在开发时使用它:

webapp.resource.loader.class = org.apache.velocity.tools.view.WebappResourceLoader
webapp.resource.loader.path = /WEB-INF/views/
webapp.resource.loader.cache = true
webapp.resource.loader.modificationCheckInterval = 2

我发现它比 SpringResourceLoader 效果更好,因为现在在我的模板中我可以包含如下内容:

#parse("/parts/header.vm")

标头的路径位于 /WEB-INF/views/header.vm 中

I had the same issue in the past, so I've written this blog entry: Spring-mvc + Velocity + DCEVM

The principal idea is to use this at least at development time:

webapp.resource.loader.class = org.apache.velocity.tools.view.WebappResourceLoader
webapp.resource.loader.path = /WEB-INF/views/
webapp.resource.loader.cache = true
webapp.resource.loader.modificationCheckInterval = 2

I've found that it works better than the SpringResourceLoader because now in my templates I can have includes like these:

#parse("/parts/header.vm")

Where the path of the header is in /WEB-INF/views/header.vm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文