How to Struts2 Clear Cache using Interceptor

Published on Author bloggerfiveLeave a comment

Custom Clear Cache Interceptor
=======================

package org.howwhywhat.interceptor;

import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class ClearCacheInterceptor  extends AbstractInterceptor{

static final Logger ClearCacheLogger = Logger.getLogger(ClearCacheInterceptor.class);
private static final long serialVersionUID = 1L;

@Override
public String intercept(ActionInvocation invocation) throws Exception {

ActionContext context=(ActionContext)invocation.getInvocationContext();

HttpServletResponse response=(HttpServletResponse)context.get(StrutsStatics.HTTP_RESPONSE);

response.setHeader(“Cache-Control”, “no-store”);
response.setHeader(“Pragma”, “no-cache”);
response.setDateHeader(“Expires”, 0);
ClearCacheLogger.info(“Cache Cleared”);
String result=invocation.invoke();
return result;
}
}

Configure Struts.xml
===============

<interceptors>

<!– Clear Cache –>
<interceptor name=”ClearCacheInterceptor”/>

<interceptor-stack name=”ClearCacheCheckStack”>
<interceptor-ref name=”ClearCacheInterceptor” />
<interceptor-ref name=”defaultStack” />
</interceptor-stack>

</interceptors>

<default-interceptor-ref name=”ClearCacheCheckStack”/>

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *