如何防止Grails缓存旧版本的gsp文件? [英] How to prevent Grails from caching old versions of gsp file?

查看:99
本文介绍了如何防止Grails缓存旧版本的gsp文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



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



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



谢谢!

解决方案

似乎没有一个简单的方法来做到这一点,但它没有太多的工作。我的解决方案是呈现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;

导入org.codehaus.groovy.grails.web.pages.GroovyPagesServlet;

public class CachingPageServlet扩展GroovyPagesServlet {

private static final String HEADER_PRAGMA =Pragma;
private static final String HEADER_EXPIRES =Expires;
private static final String HEADER_CACHE_CONTROL =Cache-Control;
$ b $ @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);
}
}

,您需要在网页中替换原件.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;

导入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请求,HttpServletResponse响应)抛出异常{
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.handleRequest(request,response);


$ / code $ / pre

你需要在grails- app / conf / spring / resources.groovy覆盖常规的Spring bean:

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

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


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!

解决方案

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.

这篇关于如何防止Grails缓存旧版本的gsp文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆