【问题标题】:Is it possible to switch to Sling Selector Script based on page property是否可以根据页面属性切换到 Sling Selector Script
【发布时间】:2015-06-21 00:07:16
【问题描述】:

在具有两种模式的 AEM Sightly 组件中;画廊和目录。目录视图通过选择器 (/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)

用户在组件对话框中有一个复选框来选择目录作为默认目录。两个视图都使用相同的 WCMUse Java 类,如果设置了默认目录属性,则会调用 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

我唯一的想法是重构/重命名脚本(称它们为 gllry.html 和 drcty.html)。检查 people_list.html 中的选择器,然后 data-sly-include 适当的脚本。但这不是对选择器使用吊索分辨率,我还不如切换到查询参数...

所以我的问题是,即使请求没有选择器,有没有办法让组件使用选择器脚本?

【问题讨论】:

    标签: java iframe aem sling sightly


    【解决方案1】:

    我认为您的想法是正确的,基于我的假设,即“用户”(来自“用户希望能够选择默认显示的视图”)指的是作者而不是最终用户网站。

    在 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)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 2020-05-04
      • 2020-05-08
      • 1970-01-01
      • 2020-01-16
      相关资源
      最近更新 更多