如何防止 Grails 缓存旧版本的 gsp 文件?

发布于 2024-08-03 12:37:33 字数 268 浏览 8 评论 0 原文

我正在对 /grails-app/views/index.gsp 进行修改。

当我保存文件并在 Firefox 中刷新 http://localhost:8080/index.gsp 时,我获取文件的旧版本。

有没有办法阻止 Grails 缓存和渲染旧版本的文件?

(我尝试重新启动服务器并清除 Firefox 的缓存。)

谢谢!

I am making modifications to /grails-app/views/index.gsp.

When I save the file and refresh http://localhost:8080/index.gsp in Firefox, I am getting an old version of the file.

Is there a way to prevent Grails from caching and rendering old versions of the file?

(I tried restarting the server and clearing Firefox's cache.)

Thanks!

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

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

发布评论

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

评论(6

随波逐流 2024-08-10 12:37:33

我们不能使用这样的过滤器吗?

class CacheFilters{

    def filters = {
        all(controller: '*', action: '*') {
            before = {
                ((HttpServletResponse) response).setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            }
            after = {

            }
            afterView = {

            }
        }
    }

}

can't we use a filter like this?

class CacheFilters{

    def filters = {
        all(controller: '*', action: '*') {
            before = {
                ((HttpServletResponse) response).setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
            }
            after = {

            }
            afterView = {

            }
        }
    }

}
你是我的挚爱i 2024-08-10 12:37:33

似乎没有一个简单的方法可以做到这一点,但工作量并不大。我的解决方案对呈现 GSP 的 servlet(以及用于非 GSP 请求的控制器)进行子类化。

这是 servlet 子类:

package com.burtbeckwith;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.groovy.grails.web.pages.GroovyPagesServlet;

public class CachingPageServlet extends GroovyPagesServlet {

   private static final String HEADER_PRAGMA = "Pragma";
   private static final String HEADER_EXPIRES = "Expires";
   private static final String HEADER_CACHE_CONTROL = "Cache-Control";

   @Override
   public void doPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setHeader(HEADER_PRAGMA, "no-cache");
      response.setDateHeader(HEADER_EXPIRES, 1L);
      response.setHeader(HEADER_CACHE_CONTROL, "no-cache");
      response.addHeader(HEADER_CACHE_CONTROL, "no-store");
      super.doPage(request, response);
   }
}

您需要替换 web.xml 中的原始文件(运行“grails install-templates”并编辑 src/templates/war/web.xml):

<servlet>
   <servlet-name>gsp</servlet-name>
   <servlet-class>com.burtbeckwith.CachingPageServlet</servlet-class>
</servlet>

并且您可能也想做同样的事情对于基于控制器的响应,因此要使用此控制器子类:

package com.burtbeckwith;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController;
import org.springframework.web.servlet.ModelAndView;

public class CachingSimpleGrailsController extends SimpleGrailsController {

   private static final String HEADER_PRAGMA = "Pragma";
   private static final String HEADER_EXPIRES = "Expires";
   private static final String HEADER_CACHE_CONTROL = "Cache-Control";

   @Override
   public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
      response.setHeader(HEADER_PRAGMA, "no-cache");
      response.setDateHeader(HEADER_EXPIRES, 1L);
      response.setHeader(HEADER_CACHE_CONTROL, "no-cache");
      response.addHeader(HEADER_CACHE_CONTROL, "no-store");
      return super.handleRequest(request, response);
   }
}

并且您需要在 grails-app/conf/spring/resources.groovy 中注册它以覆盖常规 Spring bean:

mainSimpleController(com.burtbeckwith.CachingSimpleGrailsController) {
   grailsApplication = ref('grailsApplication', true)
}

共享标头设置代码可能应该被提取到实用程序类中,而不是像我在这里那样复制/粘贴。

There doesn't seem to be a simple way to do this, but it's not much work. My solution subclasses the servlet that renders GSPs (and also the controller that's used for non-GSP requests).

Here's the servlet subclass:

package com.burtbeckwith;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.groovy.grails.web.pages.GroovyPagesServlet;

public class CachingPageServlet extends GroovyPagesServlet {

   private static final String HEADER_PRAGMA = "Pragma";
   private static final String HEADER_EXPIRES = "Expires";
   private static final String HEADER_CACHE_CONTROL = "Cache-Control";

   @Override
   public void doPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setHeader(HEADER_PRAGMA, "no-cache");
      response.setDateHeader(HEADER_EXPIRES, 1L);
      response.setHeader(HEADER_CACHE_CONTROL, "no-cache");
      response.addHeader(HEADER_CACHE_CONTROL, "no-store");
      super.doPage(request, response);
   }
}

and you'll need to replace the original in web.xml (run "grails install-templates" and edit src/templates/war/web.xml):

<servlet>
   <servlet-name>gsp</servlet-name>
   <servlet-class>com.burtbeckwith.CachingPageServlet</servlet-class>
</servlet>

and you'll probably also want to do the same for Controller-based responses, so to do that use this controller subclass:

package com.burtbeckwith;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController;
import org.springframework.web.servlet.ModelAndView;

public class CachingSimpleGrailsController extends SimpleGrailsController {

   private static final String HEADER_PRAGMA = "Pragma";
   private static final String HEADER_EXPIRES = "Expires";
   private static final String HEADER_CACHE_CONTROL = "Cache-Control";

   @Override
   public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
      response.setHeader(HEADER_PRAGMA, "no-cache");
      response.setDateHeader(HEADER_EXPIRES, 1L);
      response.setHeader(HEADER_CACHE_CONTROL, "no-cache");
      response.addHeader(HEADER_CACHE_CONTROL, "no-store");
      return super.handleRequest(request, response);
   }
}

and you'll need to register it in grails-app/conf/spring/resources.groovy to override the regular Spring bean:

mainSimpleController(com.burtbeckwith.CachingSimpleGrailsController) {
   grailsApplication = ref('grailsApplication', true)
}

The shared header-setting code should probably be extracted into a utility class instead of being copy/pasted like I did here.

将军与妓 2024-08-10 12:37:33

grails 中有一个用于控制缓存行为的插件,称为 Cache Headers:
http://grails.org/plugin/cache-headers

There's a plugin for controlling cache behavior in grails called Cache Headers:
http://grails.org/plugin/cache-headers

葮薆情 2024-08-10 12:37:33

确保您正在开发模式下运行(即 grails run-app 而不是 grails test|prod run-app,测试和生产将启用页面缓存。如果您处于开发模式,请尝试在单击时按住 Shift 键火狐刷新。

Make sure you are running in dev mode (i.e., grails run-app and not grails test|prod run-app, test and production will enable caching of the pages. If you are in dev mode, try holding the shift key when click the Firefox refresh.

稳稳的幸福 2024-08-10 12:37:33

如果您只想在开发时禁用浏览器缓存,可以使用 Firefox 的 Web Developer 插件:

https://addons.mozilla.org/en-US/firefox/addon/60

安装此插件 &选择“禁用缓存”。请记住,这将禁用所有网站的缓存。

If you simply want to disable cache for your browser while development, you can use Web Developer add on for Firefox:

https://addons.mozilla.org/en-US/firefox/addon/60

Install this add on & choose "Disable cache". Remember, that will disable caching for all the websites.

彩虹直至黑白 2024-08-10 12:37:33

仅出于开发目的,请尝试按 ctrl+F5,它也会刷新页面和缓存。用于在生产中缓存内容并提高页面查找 ui-性能插件和 grails 资源插件的性能。

For just development purposes try pressing ctrl+F5, it will refresh the page and the cache too.. for caching things on production and improving performance of page lookup ui-performance plugin and resources plugins of grails.

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