如何在 Spring MVC 中禁用 freemarker 缓存

发布于 2024-11-07 04:28:42 字数 202 浏览 1 评论 0原文

我正在使用 spring mvc v3 和 freemarker 视图,并且无法禁用缓存。 我尝试在(spring-servlet.xml)中的 viewResolver 元素中将缓存设置为 false,但没有成功。

基本上我想在 freemarker 中进行一些更改,并仅通过刷新在浏览器中查看这些更改(无需重新启动应用程序)

有任何提示如何执行此操作吗?

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?

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

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

发布评论

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

评论(8

情栀口红 2024-11-14 04:28:42

在我的 XML 中,以下内容是成功的:

<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>

这是我的邮件配置,但 freemarkerConfig 也应该对您感兴趣。

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.

岛歌少女 2024-11-14 04:28:42

我不使用 xml 配置来配置 freemarker,而是使用 @Configuration 带注释的类;因为我更喜欢 Spring-Boot' 风格。因此,您可以像这样禁用 freemarker 的缓存:

@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;
}

关键在于:

config.setCacheStorage(new NullCacheStorage());

但您也可以使用此说明:

config.setTemplateUpdateDelayMilliseconds(0);

它应该适合您。

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.

┊风居住的梦幻卍 2024-11-14 04:28:42

在应用程序属性中:

spring.freemarker.cache=false

In application.properties:

spring.freemarker.cache=false
随遇而安 2024-11-14 04:28:42

根据 手册 的定义:

如果您更改模板文件,FreeMarker 将重新加载并
下次获取模板时自动重新解析模板
时间。但是,由于检查文件是否已更改可能需要时间
消耗,有一个名为“更新”的配置级别设置
延迟''。这是自上次检查以来必须经过的时间
FreeMarker 将检查之前某个模板的较新版本
再说一遍。 默认设置为 5 秒。如果你想看
立即更改模板,将其设置为0。

。 html#setSetting%28java.lang.String,%20java.lang.String%29" rel="nofollow">freemarker.template.Configuration javadocs,位于 setSetting(key, value) 方法。
因此,简而言之,只需将配置 template_update_delay 设置为 0 即可立即检测更改。

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
  <property name="freemarkerSettings">
        <props>
            <prop key="template_update_delay">0</prop>
            <prop key="default_encoding">UTF-8</prop>
        </props>
  </property>
</bean>

As defined by the manual :

If you change the template file, then FreeMarker will re-load and
re-parse the template automatically when you get the template next
time. However, since checking if the file has been changed can be time
consuming, there is a Configuration level setting called ``update
delay''. This is the time that must elapse since the last checking for
a newer version of a certain template before FreeMarker will check
that again. This is set to 5 seconds by default. If you want to see
the changes of templates immediately, set it to 0.

After searching around, the configuration key was in the freemarker.template.Configuration javadocs, at the setSetting(key, value) method.
So, in short, just set the config template_update_delay to 0 for immediate change detection.

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  <property name="templateLoaderPath" value="/WEB-INF/ftl/"/>
  <property name="freemarkerSettings">
        <props>
            <prop key="template_update_delay">0</prop>
            <prop key="default_encoding">UTF-8</prop>
        </props>
  </property>
</bean>
遇到 2024-11-14 04:28:42

您是否检查了 FreeMarker 文档,其中包含有关如何影响模板缓存的一些提示FreeMarker 配置级别。我不确定您是否可以从 Spring MVC 内部访问 FreeMarker Configuration 对象,但如果可以,那么上面提到的文档页面可能会为您指出一个可能的解决方案。

Did you check the FreeMarker documentation, which contains some hints regarding how to influence template caching at the FreeMarker Configuration level. I'm not sure if you have access to the FreeMarker Configuration object from inside Spring MVC, but if you have, then the documentation page mentioned above could point you towards a possible solution.

随风而去 2024-11-14 04:28:42

我浪费了最后两天的时间(完全注意这个项目)试图禁用缓存。事实证明,我在 context.xml 中设置了两个选项 antiJARLocking 和 antiResourceLocking。那么模板将始终被缓存

I wasted the last two days (note entirely for this project) trying to disable the cache. It turns out I have the two options antiJARLocking and antiResourceLocking set in my context.xml. Then the templates will ALWAYS be cached

笑忘罢 2024-11-14 04:28:42

我遇到了同样的问题,只能通过实现自定义模板加载器来解决。这是工作代码:

protected void init() throws Exception {
    freemarkerConfig = new Configuration();
    freemarkerConfig.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER);
    freemarkerConfig.setTemplateLoader(new CacheAgnosticTemplateLoader(new DefaultResourceLoader(), pdfTemplatePath));
}

protected static class CacheAgnosticTemplateLoader extends SpringTemplateLoader {
    public CacheAgnosticTemplateLoader(ResourceLoader resourceLoader, String templateLoaderPath) {
        super(resourceLoader, templateLoaderPath);
    }

    @Override
    public long getLastModified(Object templateSource) {
        // disabling template caching
        return new Date().getTime();
    }
}

I had the same problem which I could solve only by implementing a custom template loader. Here is the working code:

protected void init() throws Exception {
    freemarkerConfig = new Configuration();
    freemarkerConfig.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER);
    freemarkerConfig.setTemplateLoader(new CacheAgnosticTemplateLoader(new DefaultResourceLoader(), pdfTemplatePath));
}

protected static class CacheAgnosticTemplateLoader extends SpringTemplateLoader {
    public CacheAgnosticTemplateLoader(ResourceLoader resourceLoader, String templateLoaderPath) {
        super(resourceLoader, templateLoaderPath);
    }

    @Override
    public long getLastModified(Object templateSource) {
        // disabling template caching
        return new Date().getTime();
    }
}
彻夜缠绵 2024-11-14 04:28:42

看来在最近发布的 FreeMarker 2.3.17 版本中,出现了一种合法且更简单的方法:freemarker.cache.NullCacheStorage

It seems that in the recently released FreeMarker version 2.3.17, a legal and simpler way to do it has appeared: freemarker.cache.NullCacheStorage.

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