【问题标题】:How to insert Last-Modified date in response header?如何在响应标头中插入 Last-Modified 日期?
【发布时间】:2018-12-14 10:08:49
【问题描述】:

我正在尝试将最后修改日期添加到我们的 CMS (Jahia) 提供的页面的 html 源代码中,以便它在响应标头中显示为属性。

这是我们的搜索引擎索引所必需的。

我尝试在 head 标记中添加以下语法,但没有一个允许修改的日期出现在响应标头中:

<meta name="dcterms.modified" content="Mon, 09 Apr 2018 11:41:11 GMT">
<meta name="DCTERMS.modified" content="Mon, 09 Apr 2018 11:41:11 GMT">      
<meta http-equiv="last-modified" content="Mon, 09 Apr 2018 11:41:11 GMT">   
<meta http-equiv="Last-Modified" content="Mon, 09 Apr 2018 11:41:11 GMT">   

(这些日期从 fmt:formatDate 模式 = "EEE, dd MMM yyyy HH:mm:ss z" 解析)。

我是否错误地假设在 head 标签内添加的元标签可以添加到标题中?我在 W3Schools 网站上读到 http-equiv 的唯一属性是

<meta http-equiv="content-type|default-style|refresh"> 

所以这可能是该语法不起作用的原因(尽管我可以在网络上找到对它的引用)。

提前感谢您的帮助。

【问题讨论】:

  • 或者 Jahia 专家能否向我解释如何以另一种方式将这些信息注入到标头中,而不是向模板中添加 jstl 代码?
  • 我不是 Jahia 专家,但您应该能够在 Web 服务器(Tomcat、Jetty、GlassFish、WebSphere...)配置中添加响应 HTTP 标头。另一种方法是将它们添加到源代码中,但我不确定在 Jahia 中是否可行。我认为没有办法通过 HTML/模板添加它们,HTTP 标头与 HTML 元标记不同。

标签: html http-headers jahia


【解决方案1】:

在 Jahia 支持团队的帮助下,我添加了一个带有源代码的过滤器类,以将上次修改日期添加到响应标头,并将该类添加到配置中。

  • 首先你必须添加一个弹簧配置。你可以把它放在一个xml文件中\src\main\resources\META-INF\spring

    <bean id="ResponseNewestModifFilter"   class="org.jahia.modules.lastmodif.filters.ResponseNewestModifFilter">
    <property name="priority" value="12" />
    <property name="description" value="Set Last Modif date in response header"/>
    <property name="applyOnModes" value="live,preview" />
    <property name="applyOnConfigurations" value="page" />
    </bean>
    
  • 然后添加使用addDateHeader方法的过滤器类(继承自AbstractFilter类),

    package org.jahia.modules.lastmodif.filters;
    import javax.jcr.RepositoryException;
    import org.jahia.modules.lastmodif.taglib.NewestLastModifTag;
    import org.jahia.services.render.RenderContext;
    import org.jahia.services.render.Resource;
    import org.jahia.services.render.filter.AbstractFilter;
    import org.jahia.services.render.filter.RenderChain;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class ResponseNewestModifFilter extends AbstractFilter {
    
    private static final Logger logger = LoggerFactory
            .getLogger(ResponseNewestModifFilter.class);
    
    @Override
    public String execute(String previousOut, RenderContext renderContext,
            Resource resource, RenderChain chain) throws Exception {
        try {
            if (renderContext.getResponse().getHeader("Last-Modified") != null) {
                renderContext.getResponse().setDateHeader(
                        "Last-Modified",
                        NewestLastModifTag.getNewestLastModifDateOfPage(
                                resource.getNode()).getTime());
            } else {
                renderContext.getResponse().addDateHeader(
                        "Last-Modified",
                        NewestLastModifTag.getNewestLastModifDateOfPage(
                                resource.getNode()).getTime());
            }
        } catch (RepositoryException ex) {
            logger.error("Error set Last-Modified reponse header", ex);
        }
        return previousOut;
       }
    }
    

这个类引用了一个自定义标签库(NewestLastModifTag),它确保查询所有子节点以获取最后修改日期

package org.jahia.modules.lastmodif.taglib;

import java.util.Calendar;

import javax.jcr.RepositoryException;

import org.jahia.services.content.JCRNodeIteratorWrapper;
import org.jahia.services.content.JCRNodeWrapper;

public class NewestLastModifTag {

    public static java.util.Date getNewestLastModifDateOfPage(org.jahia.services.content.JCRNodeWrapper node) throws RepositoryException {

        if (node.hasNodes()) {
            return getSubnodesWithNewerDate(node, node.getProperty("jcr:lastModified").getDate()).getTime();
        }
        return node.getProperty("jcr:lastModified").getDate().getTime();
    }

    private static Calendar getSubnodesWithNewerDate(JCRNodeWrapper node, Calendar date) throws RepositoryException {

        JCRNodeIteratorWrapper nodes = node.getNodes();
        while (nodes.hasNext()) {
            JCRNodeWrapper snode = (JCRNodeWrapper)nodes.next();
            if (snode.isNodeType("jnt:page")) {
                continue;
            }
            if (snode.hasProperty("jcr:lastModified") && snode.getProperty("jcr:lastModified").getDate().after(date)) {
                date = snode.getProperty("jcr:lastModified").getDate();
            }
            date = getSubnodesWithNewerDate(snode, date);
        }
        return date;
    }

}

【讨论】:

    【解决方案2】:

    您可以将任何元数据包含到 Jahia 为您的页面生成的 head html 中。 以下是我们页面之一的 html 输出示例:

    <meta name="dcterms.created" content="Mon May 26 08:06:56 CEST 2018" />
    <meta name="dcterms.modified" content="Tue Oct 30 10:40:43 CET 2018" />
    <meta name="dcterms.issued " content="Wed Oct 31 09:09:53 CET 2018" />
    

    为了检索它们,您需要使用当前页面节点:

    <c:set var="pageNode" value="${jcr:getParentOfType(currentNode, 'jnt:page')}"/>
    <c:if test="${empty pageNode}">
        <c:choose>
            <c:when test="${jcr:isNodeType(renderContext.mainResource.node, 'jnt:page')}">
                <c:set var="pageNode" value="${renderContext.mainResource.node}"/>
            </c:when>
            <c:otherwise>
                <c:set var="pageNode" value="${jcr:getParentOfType(renderContext.mainResource.node, 'jnt:page')}"/>
            </c:otherwise>
        </c:choose>
    </c:if>
    

    根据 Jahia API,您可以检索以下页面属性:

    <c:set var="dateOfCreation" value="${pageNode.creationDateAsDate}" />
    <c:set var="dateOfLastModification" value="${pageNode.lastModifiedAsDate}" />
    <c:set var="dateOfLastPublication" value="${pageNode.lastPublishedAsDate}" />
    

    然后将它们输出到您的组件视图或模板中:

    <c:if test="${!empty dateOfCreation}"><meta name="dcterms.created" content="${fn:escapeXml(dateOfCreation)}" /></c:if>
    <c:if test="${!empty dateOfLastModification}"><meta name="dcterms.modified" content="${fn:escapeXml(dateOfLastModification)}" /></c:if>
    <c:if test="${!empty dateOfLastPublication}"><meta name="dcterms.issued " content="${fn:escapeXml(dateOfLastPublication)}" /></c:if>
    

    【讨论】:

    • 感谢您的回答。但是,我的问题是没有将相关日期放入 html 代码中,而是放入响应标头中。正如 mikiqex 上面指出的,这不是一回事。我终于添加了一个过滤器,请参阅下面的答案。
    猜你喜欢
    • 2010-12-28
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2014-04-25
    • 1970-01-01
    相关资源
    最近更新 更多