Adobe CQ。获取当前页面路径

时间:2014-06-10 10:15:30

标签: java jsp jstl adobe cq5

我需要将当前页面路径设置为canocical标记。不幸的是我在这里有一些错误。在这段代码上:

<%
ResourceResolver resolver = slingRequest.getResourceResolver();
Externalizer externalizer = resolver.adaptTo(Externalizer.class);
String canonUrl = externalizer.publishLink(resourceResolver, "http", currentPage.getPath());
%>
<link rel="canonical" href="${canonUrl}" />

我有这个输出:

<link rel="canonical" href="" />

出了什么问题。但是如果我像这样设置canonical标签:

<link rel="canonical" href="${currentPage.path}" />

我有这个输出:

<link rel="canonical" href="/content/example/eu/germany/de_de/about.html" />

那几乎没问题。而现在,我只需要添加域名( http://example.com/content/example/eu/germany/de_de/about.html )。但是如何在没有硬编码的情况下做到这一点?方法在本地化页面上也可以正常工作。

2 个答案:

答案 0 :(得分:3)

您需要在Externalizer Service中配置域。 转到OSGi控制台(/ system / console / components)并查找com.day.cq.commons.impl.ExternalizerImpl。然后单击configure并为本地,作者和发布添加行。例如:

local http://localhost:4502
author http://author.example.com
publish http://publish.domain.com

答案 1 :(得分:1)

你可以通过jstl -

从bean类中读取主机
private String getHost(HttpServletRequest request)
  {
    String xHost = request.getHeader("x-forwarded-host");
    if(xHost == null)
    {
        String host = request.getHeader("Host");
        if(host == null)
        {
            String urlS = request.getRequestURL().toString();
            try
            {
                return (new URI(urlS)).getAuthority();
            }
            catch(URISyntaxException use)
            {
                return (new 
              StringBuilder()).append(request.getServerName())
               .append(":").append(request.getServerPort()).toString();
            }
        } else
        {
            return host;
        }
       }
       if(xHost.indexOf(',') >= 0)
        xHost = xHost.substring(xHost.indexOf(','));
    return xHost.trim();
   }