2

PF 3.5(4.0), Omnifaces 1.6.3, Mojara 2.1.21

Is it possible to control http headers which will be sent inside of JSF xhtml page ? I mean something like:

.xhtml:

<html xmlns:http="a cool name space">

  <h:head>
    <http:headers header="Cache-Control" value="no-cache, no-store, must-revalidate" />
  </h:head>
  <h:body> .... </h:body>
</html>
Xtreme Biker
  • 28,480
  • 12
  • 120
  • 195
Tony
  • 2,016
  • 3
  • 27
  • 50
  • 2
    FYI: OmniFaces 1.7 will feature a `CacheControlFilter` for the very purpose. It's already in current snapshots. Javadoc here: http://wiki.omnifaces.googlecode.com/hg/javadoc/org/omnifaces/filter/CacheControlFilter.html – BalusC Nov 28 '13 at 11:07
  • Thank you, interesting and useful feauture of OmniFaces. – Tony Nov 28 '13 at 13:13

2 Answers2

6

You mean not to instruct the browser for caching it? Just use a filter and add what you want to your response header:

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

See also:

Community
  • 1
  • 1
Xtreme Biker
  • 28,480
  • 12
  • 120
  • 195
  • Thank you, I hope the headers that are set in filter will not be overriden by JSF? – Tony Nov 28 '13 at 13:15
  • Actually they aren't. If you watch the network traffic using a tool like firebug (you'll find [this](http://stackoverflow.com/questions/5374100/how-to-debug-the-http-response-headers-from-a-http-call) insteresting), you'll notice the reponse header has the *Cache-Control* field now. – Xtreme Biker Nov 28 '13 at 14:27
2

I found a simple solution by adding the line below to your XHTML page:

  <f:event type="preRenderView"
    listener="#{facesContext.externalContext.response.setHeader('Cache-Control', 'no-cache, no-store')}" />
Ta Ntt
  • 101
  • 4