【问题标题】:How to pass the url dynamically in the data-sly-resource?如何在 data-sly-resource 中动态传递 url?
【发布时间】:2022-02-11 01:30:39
【问题描述】:

我有 404.html 页面,其内容如下:

<div data-sly-resource="/content/emc/en-us/errorhandler/404" data-sly-unwrap></div>

还有一个java类如下:

package apps.sling.servlet.errorhandler;

import com.adobe.cq.sightly.WCMUse;

public class ResponseStatus extends WCMUse {
    
    @Override
    public void activate() throws Exception {
        getResponse().setStatus(404);
    }
}

在状态 404 上,它进入 404 页面 (/content/emc/en-us/errorhandler/404)。我的要求是当用户使用 en-us url 时,它应该转到 /content/emc/en-us/errorhandler/404。如果用户在 zh-cn url 上,它应该转到 /content/emc/zh-cn/errorhandler/404。我需要通过读取 url 上的语言环境在 data-sly-resource 中动态传递 url。谁能帮我写剧本?

【问题讨论】:

    标签: java html aem


    【解决方案1】:

    让我分享一下方法,它可能会帮助你。

    编写一个辅助类:

    import com.adobe.cq.sightly.WCMUsePojo;
    import com.day.cq.wcm.api.Page;
    
    public class PageNavigationHelper extends WCMUsePojo {
        private String locale;
        @Override
        public void activate() throws Exception {
            this.locale = get("locale", String.class);
        }
        public String getNotFoundUrl(){
            Page currentPage = getCurrentPage();
            String currentLocale = currentPage.getLanguage(true).getLanguage();
    
            // you can built more language specific url here.
            if(currentLocale.equalIgnoreCase("en")){
                return "/content/emc/en-us/errorhandler/404";
            }else{
                return "/content/emc/zh-cn/errorhandler/404";
            }
        }
    }
    

    然后进入你的 HTML:

    <div data-sly-use.langHelper="${'com.service.helper.PageNavigationHelper'}" data-sly-unwrap>
        <div data-sly-resource="${langHelper.getNotFoundUrl}" data-sly-unwrap></div>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 2014-10-13
      • 1970-01-01
      • 2014-11-04
      相关资源
      最近更新 更多