是否可以根据页面 属性 切换到 Sling Selector Script

Is it possible to switch to Sling Selector Script based on page property

在 AEM Sightly 组件中,它有两种模式;画廊和目录。目录视图是用 select 或 (/apps/mi-proj/people_list/directory.html) 实现的。

默认情况下,组件以图库模式呈现(使用 people_list/people_list.html)。用户希望能够选择默认显示哪个视图。在任何一种情况下,用户都可以在视图之间切换。

示例路径假定内容 sling:resourceType = people_list:

/content/mi-proj/people.html (people_list.html)
/content/mi-proj/people.gallery.html (people_list.html)
/content/mi-proj/people.directory.html (directory.html)

用户在组件对话框中有一个复选框,将select目录作为默认目录。这两个视图使用相同的 WCMUse Java class,如果设置了默认目录 属性,则调用 this.getResponse().sendRedirect(redirectPath)。

private void forwardToDirectory(String selector){
    String redirectPath;
    redirectPath = String.format("%s.%s.html", this.getCurrentPage().getPath(), selector);          
    try {
        this.getResponse().sendRedirect(redirectPath);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

重定向在我的本地主机和开发层上工作正常。但是在 AEM 的内容查找器页面 (cf#) 中呈现页面时会出现问题, /cf#/content/mi-proj/people.html

它将页面放置在一个大的 iframe 中。由于某种原因,iframe 方案 (http) 与重定向的请求 (https) 不匹配,因此浏览器抛出异常(我已经尝试将其强制为 https,但它仍然失败,表示 https 方案与iframe 方案、数据)...看来我需要解决这个问题并取消重定向...

无论如何我都不喜欢强制 302 重定向,并且更愿意在后端做一些事情来处理它...这是我想写的过程...

if directoryDefault == true || selector == 'directory'
    use directory.html
else 
    people_list.html

我唯一的想法是 refactoring/renaming 脚本(称它们为 gllry.html 和 drcty.html)。检查 select 或 people_list.html,然后数据巧妙地包含适当的脚本。但这并没有使用 selectors 的吊索分辨率,我不妨切换到查询参数...

所以我的问题是,有没有什么方法可以让组件使用 selector 脚本,即使请求没有 selector?

我认为你的想法是正确的,基于我的假设 "users"(来自 "Users would like the ability to choose which view is shown by default")指的是作者而不是网站的最终用户。

在 people_list.html 中,检查 属性 并在目录模式下渲染(如果通过包含 directory.html 设置)。否则,包括 gallery.html:

<div data-sly-test.directoryDefault="${properties.directoryDefault == true}" data-sly-unwrap>
    <div data-sly-include="directory.html" data-sly-unwrap />
</div>
<div data-sly-test="${!directoryDefault}" data-sly-unwrap>
    <div data-sly-include="gallery.html" data-sly-unwrap />
</div>

无论 属性 值如何,您仍然可以使用选择器访问任一视图:

/content/mi-proj/people.html (gallery.html or directory.html)
/content/mi-proj/people.gallery.html (gallery.html)
/content/mi-proj/people.directory.html (directory.html)

有关何时在选择器上使用查询参数的更多详细信息,请查看此 post:http://www.citytechinc.com/us/en/blog/2013/08/apache-sling-selectors-request-parameters.html