aem中如何获取组件jsp/html中的网站域名?

How to get the website domain name in a component's jsp/html in aem?

我需要在我的组件 jsp/html using sightly 中获取网站的域名。

假设页面 url 是“https://test.com/en.html", I just need to get "https://test.com”。如何在不使用任何 java 代码的情况下实现它?

有什么直接的方法可以使用吗?

服务器端

如果您想在服务器端获取域名(即让它显示在初始响应中发送的呈现 HTML 中)。

global objects available to use in HTL (formerly known as sightly) is the request object. This object is a SlingHttpServletRequest, so you should be able to use the serverName property. (I believe this object is also available in JSPs if use the <cq:defineObjects> described here)

之一
${request.serverName}

客户端

如果您想在客户端(即在加载页面的人的浏览器中)获取域名。

您可以使用 Javascript 中的普通 window.location 对象来获取主机名:

<script>
    let domainName = window.location.hostname;
    //do the JS stuff you want to with the domain name
</script>

检查 Location docs 以获得 window.location 的所有可用属性。