2012-08-17 3 views
1

캐싱을 방지하기 위해 인터셉터를 작성했지만 페이지는 여전히 캐시됩니다.clear cache interceptor struts2가 작동하지 않습니다.

인터셉터 :

public class ClearCacheInterceptor implements Interceptor { 
    public String intercept(ActionInvocation invocation)throws Exception{ 
     String result = invocation.invoke(); 

     ActionContext context = (ActionContext) invocation.getInvocationContext(); 
     HttpServletRequest request = (HttpServletRequest) context.get(StrutsStatics.HTTP_REQUEST); 
     HttpServletResponse response=(HttpServletResponse) context.get(StrutsStatics.HTTP_RESPONSE); 

     response.setHeader("Cache-Control", "no-store"); 
     response.setHeader("Pragma", "no-cache"); 
     response.setDateHeader("Expires", 0); 

     return result; 
    } 

    public void destroy() {} 
    public void init() {} 
} 

Struts.xml

<struts> 
    <constant name="struts.devMode" value="true"/> 
    <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 

    <package name="default" extends="struts-default"> 
    <interceptors> 
     <interceptor name="caching" class="com.struts.device.interceptor.ClearCacheInterceptor"/> 
     <interceptor-stack name="cachingStack">  
     <interceptor-ref name="caching" />  
     <interceptor-ref name="defaultStack" />  
     </interceptor-stack> 
    </interceptors> 

    <action name="Login" class="struts.device.example.LogIn"> 
     <interceptor-ref name="cachingStack"/> 
     <result>example/Add.jsp</result> 
     <result name="error">example/Login.jsp</result> 
    </action> 
    </package> 
</struts> 

응용 프로그램이 잘 작동; 인터셉터를 실행하지만 캐싱을 방지하지는 않습니다.

+0

는 뜻 "그것은 여전히 ​​캐시 있어요?" –

+0

예 ..... @ DaveNewton – Srishti123

+0

HTTP 헤더 (Cache-Control, Pragma 및 Expires)가 설정되어 있는지 확인하기 위해 Firebug 또는 Chrome의 개발자 도구를 사용하여 응답을 봅니다. –

답변

1

나는 내 문제를 해결했습니다. 추적 도구를 제공하는 개발자 도구 덕분입니다.

내 코드에 약간의 순서 변경

나를 도와 : 결과가 invocation.invoke() 반환되기 전에 을 렌더링 the Struts 2 interceptor docs에 따라. 결과가 클라이언트에 렌더링되기 전에 헤더를 설정하면 반환 된 결과에 헤더가 설정됩니다.

즉, "이 캐시를 삭제 아니에요"으로

public String intercept(ActionInvocation invocation)throws Exception{ 
    HttpServletResponse response = ServletActionContext.getResponse(); 

    response.setHeader("Cache-Control", "no-store"); 
    response.setHeader("Pragma", "no-cache"); 
    response.setDateHeader("Expires", 0); 

    return invocation.invoke(); 
} 
관련 문제