如何在组件级别访问 URL 选择器
How to access to an URL selector at a component level
场景:(AEM 6.3.2) 我正在请求带有选择器 "test1" 的页面,如下所示:
http://localhost:4502/content/myapp/home.test1.html
此页面有一个 parsys,我在其中放置了一个组件 "slider",因此该组件的路径是:“/content/myapp/home/jcr:content/parsys/slider”
在 "slider" 组件级别,如何访问 "test1" 选择器?
我尝试了不同的方法(SlingModel、WCMUsePojo、"request" HTL 全局对象...),但总是遇到同样的问题:我可以访问的 "request" 是 GET 请求组件 (GET "/content/myapp/home/jcr:content/parsys/slider.html") 其中选择器不存在。
您应该使用继承自 HttpServletRequest
的方法 SlingHttpServletRequest##getPathInfo
在您的示例中,如果您请求:
http://localhost:4502/content/myapp/home.test1.html
然后在你的组件的 Class (Use/SlingModel) 中你可以调用 request.getPathInfo()
这将 return: /content/myapp/home.test1.html
然后您可以使用以下方法解析该路径:com.day.cq.commons.PathInfo
这里是一个吊索模型示例:
package com.mycom.core.models;
import com.day.cq.commons.PathInfo;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
@Model(adaptables = SlingHttpServletRequest.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class SampleModel {
@Self
SlingHttpServletRequest request;
public PathInfo getPathInfo() {
return new PathInfo(request.getPathInfo());
}
}
然后在您的 HTML 中您可以:
<sly data-sly-use.sample="com.mycom.core.models.SampleModel"/>
<div>${sample.pathInfo.selectors @ join=', '}</div>
将输出:(基于您的示例路径)
<div>test1</div>
刚刚在另一个 AEM 实例(相同版本)上检查了完全相同的 component/code,它正在运行...将检查可能导致错误行为的原因,但我想问题已解决!
场景:(AEM 6.3.2) 我正在请求带有选择器 "test1" 的页面,如下所示: http://localhost:4502/content/myapp/home.test1.html
此页面有一个 parsys,我在其中放置了一个组件 "slider",因此该组件的路径是:“/content/myapp/home/jcr:content/parsys/slider”
在 "slider" 组件级别,如何访问 "test1" 选择器?
我尝试了不同的方法(SlingModel、WCMUsePojo、"request" HTL 全局对象...),但总是遇到同样的问题:我可以访问的 "request" 是 GET 请求组件 (GET "/content/myapp/home/jcr:content/parsys/slider.html") 其中选择器不存在。
您应该使用继承自 HttpServletRequest
的方法SlingHttpServletRequest##getPathInfo
在您的示例中,如果您请求:
http://localhost:4502/content/myapp/home.test1.html
然后在你的组件的 Class (Use/SlingModel) 中你可以调用 request.getPathInfo()
这将 return: /content/myapp/home.test1.html
然后您可以使用以下方法解析该路径:com.day.cq.commons.PathInfo
这里是一个吊索模型示例:
package com.mycom.core.models;
import com.day.cq.commons.PathInfo;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.Self;
@Model(adaptables = SlingHttpServletRequest.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class SampleModel {
@Self
SlingHttpServletRequest request;
public PathInfo getPathInfo() {
return new PathInfo(request.getPathInfo());
}
}
然后在您的 HTML 中您可以:
<sly data-sly-use.sample="com.mycom.core.models.SampleModel"/>
<div>${sample.pathInfo.selectors @ join=', '}</div>
将输出:(基于您的示例路径)
<div>test1</div>
刚刚在另一个 AEM 实例(相同版本)上检查了完全相同的 component/code,它正在运行...将检查可能导致错误行为的原因,但我想问题已解决!