我可以建议结合使用 uri 上下文并从服务器端向资源 URL 添加 .html 扩展名。
- 它不适用于资源链接,例如DAM 中的 PDF 文件。
使用@ context = 'uri',href 和src 属性的默认上下文,并且不显式添加.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>
你不能在同一个元素中同时使用@extension 和@context。
您可以像 <a href="${path}.html">Title</a> 这样附加 .html,或者更好的方法是在 sling 模型级别解决这个问题,可能是这样的 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' - 完全禁用 xss 保护。
检查this 以获取可用的上下文表达式选项。