gpt4 book ai didi

aem - 组件的jsp/html aem6.3中如何获取不带域名的浏览器页面URL?

转载 作者:行者123 更新时间:2023-12-02 17:04:49 26 4
gpt4 key购买 nike

假设我的页面 url 是 www.test.com/myproject/en/test.html。现在我需要在组件的 jsp/html 文件中获取“/myproject/en/test.html”。我们正在使用。

我正在尝试使用下面的代码来实现它-

<script>
var x = window.location.pathname;
alert(x);
</script>

我在变量 x 中获取值 /myproject/en/test.html,但无法使用元标记中的值,如下所示-

<link rel="icon" href="https://mywebsite.com ${x}">

如何在元标记中传递 x 的值?或者,还有其他方法可以实现吗?

最佳答案

您可以直接使用Sightly提供的请求对象(Java-backed Objects)。

完整网址:

${request.requestURL.toString}

对于当前页面路径:

${currentPage.path} // returns only the path without domain name

对于 URL 的特定部分:

${request.scheme} // returns https or http
${request.serverName} // returns server name eg: stackoverflow.com
${request.serverPort} // returns server port eg: 4502 for AEM
${request.requestURI} // returns URI eg: /content/test.html

连接特定部分以在 Sightlty 中创建您想要的 url:

<sly data-sly-test.scheme="${request.scheme}"/>
<sly data-sly-test.servername="${request.serverName}"/>
<sly data-sly-test.serverport="${request.serverPort}"/>
<sly data-sly-test.uri="${request.requestURI}"/>

<link rel="icon" href="${scheme}://${servername}:${serverport}${uri}">

请求对象下可用的方法:https://sling.apache.org/apidocs/sling7/org/apache/sling/api/SlingHttpServletRequest.html

其他有用的 HTL 全局对象: https://docs.adobe.com/content/help/en/experience-manager-htl/using/htl/global-objects.html

关于aem - 组件的jsp/html aem6.3中如何获取不带域名的浏览器页面URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52220447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com