no

How to implement no caching on JSF pages

Currently there are 2 ways I know that can do this. 1.) By implementing Filter and manually overriding the cache controls. See code below ...

Currently there are 2 ways I know that can do this.

1.) By implementing Filter and manually overriding the cache controls. See code below (note: not my original I found this somewhere else long time ago):
@WebFilter(servletNames = { "Faces Servlet" })
public class NoCacheFilter implements Filter {

 @Override
 public void doFilter(ServletRequest req, ServletResponse res,
   FilterChain chain) throws IOException, ServletException {
  HttpServletRequest request = (HttpServletRequest) req;
  HttpServletResponse response = (HttpServletResponse) res;

  if (!request.getRequestURI().startsWith(
    request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {
   // Skip JSF resources (CSS/JS/Images/etc)
   response.setHeader("Cache-Control",
     "no-cache, no-store, must-revalidate"); // HTTP 1.1.
   response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
   response.setDateHeader("Expires", 0); // Proxies.
  }

  chain.doFilter(req, res);
 }

 @Override
 public void init(FilterConfig filterConfig) throws ServletException {

 }

 @Override
 public void destroy() {

 }

}

2.) The one I preferred, using omnifaces' CacheControlFilter. More information available at: http://showcase.omnifaces.org/filters/CacheControlFilter. To use just need to add the filter and integrate with facesServlet.
<filter>
 <filter-name>noCache</filter-name>
 <filter-class>org.omnifaces.filter.CacheControlFilter</filter-class>
</filter>

<filter-mapping>
 <filter-name>noCache</filter-name>
 <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

Related

jsf 4130151786495071550

Post a Comment Default Comments

item