Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Velocity Template auto-reload doesn't work

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>
like image 805
Mohsen Avatar asked Sep 04 '11 09:09

Mohsen


People also ask

What is Velocityengine in Java?

The Apache Velocity Engine is a free open-source templating engine. Velocity permits you to use a simple yet powerful template language to reference objects defined in Java code. It is written in 100% pure Java and can be easily embedded into your own applications.

What is velocity .VM file?

Velocity is a Java-based templating engine. It's an open source web framework designed to be used as a view component in the MVC architecture, and it provides an alternative to some existing technologies such as JSP. Velocity can be used to generate XML files, SQL, PostScript and most other text-based formats.

What is VelocityContext?

VelocityContext(Context innerContext) Chaining constructor, used when you want to wrap a context in another. VelocityContext(Map<String,Object> context) Creates a new instance with the provided storage (and no inner context).


1 Answers

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.

like image 107
serg Avatar answered Sep 20 '22 12:09

serg