【问题标题】:How to control http headers in JSF?如何控制 JSF 中的 http 标头?
【发布时间】:2013-12-14 08:38:28
【问题描述】:

PF 3.5(4.0)、Omnifaces 1.6.3、Mojara 2.1.21

是否可以控制将在 JSF xhtml 页面内发送的 http 标头?我的意思是:

.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>

【问题讨论】:

标签: jsf servlets jsf-2 http-headers omnifaces


【解决方案1】:

你的意思是不指示浏览器缓存它?只需使用过滤器并将您想要的内容添加到响应标题中:

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.
}

另请参阅:

【讨论】:

  • 谢谢,希望filter里设置的headers不要被JSF覆盖?
  • 其实不是。如果您使用 firebug 之类的工具观察网络流量(您会发现 this insteresting),您会注意到响应标头现在具有 Cache-Control 字段。
【解决方案2】:

我通过在您的 XHTML 页面中添加以下行找到了一个简单的解决方案:

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

【讨论】:

    猜你喜欢
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 2017-06-01
    相关资源
    最近更新 更多