Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable freemarker caching in Spring MVC

I'm using spring mvc v3 with freemarker views and cannot disable caching. I tried by setting cache to false in viewResolver element in (spring-servlet.xml) but didn't work.

Basically what I'd like to the do some changes in freemarker and see these changes in the browser with refresh only (w/o restarting the application)

Any hints how to do that?

like image 925
Bilgin Ibryam Avatar asked May 12 '11 16:05

Bilgin Ibryam


2 Answers

In my XML the following was successful:

<bean id="freemarkerMailConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
    <property name="templateLoaderPaths" value="classpath:emailtemplates/task,classpath:emailtemplates/user"/>
    <!-- Activate the following to disable template caching -->
    <property name="freemarkerSettings" value="cache_storage=freemarker.cache.NullCacheStorage" />
</bean>

This is my mail config, but the freemarkerConfig should be interesting four you, too.

like image 138
Sebastian Janzen Avatar answered Sep 18 '22 01:09

Sebastian Janzen


I dont use to configure freemarker with xml configurations but with @Configuration annotated classes; cause i rather the Spring-Boot´ style. So you can disable the freemarker´s cache like this:

@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() throws IOException, TemplateException
{
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer()
    {

        @Override
        protected void postProcessConfiguration(freemarker.template.Configuration config) throws IOException, TemplateException
        { 
            ClassTemplateLoader classTplLoader = new ClassTemplateLoader(context.getClassLoader(), "/templates"); 
            ClassTemplateLoader baseMvcTplLoader = new ClassTemplateLoader(FreeMarkerConfigurer.class, ""); //TODO tratar de acceder a spring.ftl de forma directa
            MultiTemplateLoader mtl = new MultiTemplateLoader(new TemplateLoader[]
            { 
                classTplLoader,
                baseMvcTplLoader
            });
            config.setTemplateLoader(mtl);
            config.setCacheStorage(new NullCacheStorage()); 
        }
    };
    configurer.setDefaultEncoding("UTF-8"); 
    configurer.setPreferFileSystemAccess(false);
    return configurer;
}

The key is in:

config.setCacheStorage(new NullCacheStorage());

But you can also use this instruction instead:

config.setTemplateUpdateDelayMilliseconds(0);

It should work for you.

like image 27
EliuX Avatar answered Sep 19 '22 01:09

EliuX