繁体   English   中英

克服AEM 6.3 Sightly / HTL中的@extension ='html'`困难的惯用方式?

[英]Idiomatic way to overcome difficulties with `@extension = 'html'` in AEM 6.3 Sightly/HTL?

AEM的HTL(fka Sightly)具有用于重新格式化URL属性的特殊用法,例如

<a href="${properties.path @ extension = 'html'}">

这个成语的目的是双重的

  1. .html附加到通过pathbrowser字段创作的内部链接
  2. 应用已配置为剥离/content/projectname资源映射

不幸的是,这个精心设计的功能有几个问题:

  • 它不适用于资源链接,例如DAM中的PDF文件。
  • 它不适用于不以.html结尾的外部链接。
  • 它会在包含查询字符串参数的URL中转义“&”,从而断开链接。

我的团队现在的任务是修复由于过度使用此extension = 'html'技巧而导致的许多缺陷,我们希望以最小的回归风险一致,快速地修复所有缺陷。

是否有快速解决方案,最好是可以通过无意识搜索/替换每次出现的extension = 'html'重复进行extension = 'html'

我可以建议结合使用uri上下文和从服务器端向资源URL添加.html扩展名。

  • 它不适用于资源链接,例如DAM中的PDF文件。

使用@ context = 'uri'hrefsrc属性的默认上下文,并且不显式添加.html扩展名。

输入 -

<a href="${'/content/dam/repl/en.pdf'}">Resource Link</a>使用默认的uri上下文。

输出 -

<a href="/content/dam/repl/en.pdf">Resource Link</a>

在任何其他html属性上,使用属性上下文- @ context='attribute'

输入 -

<div data-link="${'/content/dam/repl/en.pdf' @ context='attribute'}"/>

输出 -

<div data-link="/content/dam/repl/en.pdf"/>


  • 它不适用于不以.html结尾的外部链接。
  • 它会在包含查询字符串参数的URL中转义“&”,从而断开链接。

再次使用@ context = 'uri' ,不会在URL中转义 ,也可以与选择器和#参数一起使用。 增加了XSS保护的优势。

输入 -

<a href="${'http://www.reddit.com.selector1.selector2?a=1&b=2&c=3'}">URI context</a>

输出 -

<a href="http://www.reddit.com.selector1.selector2?a=1&b=2&c=3">URI context</a>

  • 将.html附加到内部资源URL

您不能在同一元素中同时使用@ extension和@ context。 您可以像这样<a href="${path}.html">Title</a>附加.html,或者更好的方法是在吊索模型级别解决此问题,也许是一种util方法。

public static String getUrl(String link, String extension, ResourceResolver resourceResolver) {
        String updatedLink = "";
        if (link != null) {
            Resource pathResource = resourceResolver.getResource(link);
            // check if resource exists
            if (pathResource != null) {
                // append .html
                updatedLink = resourceResolver.map(link) + extension;
            }
        }
        return updatedLink;
    }

旁注:出于明显原因,避免使用@ context='unsafe' unsafe'-完全禁用xss保护。

选中选项以获取可用的上下文表达式选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM