在 AEM Sightly 组件中,它有两种模式;画廊和目录。目录 View 是使用选择器 (/apps/mi-proj/people_list/directory.html) 实现的。
默认情况下,组件以图库模式呈现(使用 people_list/people_list.html)。用户希望能够选择默认显示哪个 View 。在任何一种情况下,用户都可以在 View 之间切换。
假设内容 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)
用户在组件对话框中有一个复选框来选择目录作为默认目录。两个 View 都使用相同的 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 中的选择器,然后数据巧妙地包含适当的脚本。但这并没有对选择器使用 sling 分辨率,我不妨切换到查询参数...
所以我的问题是,有没有办法让组件使用选择器脚本,即使请求没有选择器?
最佳答案
我认为您的想法是正确的,基于我的假设“用户”(来自“用户希望能够选择默认显示哪个 View ”)指的是作者而不是最终用户网站。
在 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>
无论属性值如何,您仍然可以使用选择器访问任一 View :
/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
关于java - 是否可以根据页面属性切换到 Sling Selector Script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30960058/