aem - 重定向实现

标签 aem sling sightly

我正在尝试将重定向功能从 scriptlet 转移到 Sightly 类。 到目前为止我做了什么:

package apps.myproject.components.page.generic;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.wcm.foundation.ELEvaluator;
import com.adobe.cq.sightly.WCMUse;

public class PageComponent extends WCMUse {

    private static final Logger log = LoggerFactory.getLogger(PageComponent.class);

    public void activate() throws Exception {

    }

    public void redirect() throws IOException{
        String location = getProperties().get("redirectTarget", "");

        // resolve variables in path
        //where I can find PageContext Object?
        //location = ELEvaluator.evaluate(location, getRequest(), new PageContext());

        boolean wcmModeIsDisabled = getWcmMode().isDisabled();
        boolean wcmModeIsPreview = getWcmMode().isPreview();
        if ( (location.length() > 0) && ((wcmModeIsDisabled) || (wcmModeIsPreview)) ) {
            // check for recursion
            if (getCurrentPage() != null && !location.equals(getCurrentPage().getPath()) && location.length() > 0) {
                // check for absolute path
                final int protocolIndex = location.indexOf(":/");
                final int queryIndex = location.indexOf('?');
                final String redirectPath;
                if ( protocolIndex > -1 && (queryIndex == -1 || queryIndex > protocolIndex) ) {
                    redirectPath = location;
                } else {
                    redirectPath = getResourceResolver().map(getRequest(), location) + ".html";
                }
                getResponse().sendRedirect(redirectPath);
            } else {
                getResponse().sendError(HttpServletResponse.SC_NOT_FOUND);
            }
        }
    }
}

问题是我不知道应该从哪里获取 ELEvaluator.evaluate() 方法的 PageContext 对象。传递 null 抛出 NullPointerException,传递 new PageContext() 抛出

java.lang.Error: Unresolved compilation problem: 
    Cannot instantiate the type PageContext

如何在 Sightly 类中使​​用 ELEvaluator 或类似的东西?有什么想法吗?

最佳答案

不太确定它是否回答了您的问题,但对我来说 ELEvaluator.evaluate 是评估存储在您的 redirectTarget 属性中的潜在表达式。如果您不寻找边缘情况,您可以忽略整行并专注于您的重定向。

  • 阅读属性(property)
  • 验证和外部化
  • response.sendRedirect(链接)

关于aem - 重定向实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28747658/

相关文章:

java - Sling - 获取资源的属性

aem - 如何在页面组件中包含 AEM parsys

aem - HTL 无需 Getter 即可访问属性

java - 重写 Sling DefaultGetServlet.java

angular - 在angular2中动态加载HTML模板

java - 无法获取类型为 'interface org.apache.sling.rewriter.Transformer' 的类 'linkrewriter' 的组件

rhino - 调试 Sling/Sightly 服务器端 JavaScript

aem - 根据RTF小部件的输入以HTML呈现电话链接

单击复选框时,AEM 中的 Javascript 添加类

java - 如何使 SlingHttpServletRequest.getParts() 在 JUnit Test 中返回正确的值?