Aem 重定向实现

Aem 重定向实现,aem,sling,sightly,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.Log

我试图将重定向功能从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);
            }
        }
    }
}
问题是我不知道应该从哪里获取elevator.evaluate()方法的PageContext对象。传递
null
会引发我
NullPointerException
,传递
new PageContext()
会引发

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

如何在课堂上使用升降器或类似的东西?有什么想法吗?

不确定它是否回答了您的问题,但对我来说,
elevator.evaluate
是评估存储在
重定向目标
属性中的潜在表达式。如果你不是在寻找一个边缘案例,你可以忽略整条线,专注于你的重定向

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

可能重复@AshleyMedway不,它绝对不是重复。我的问题是,在没有pageContext对象的情况下,如何在Sightly类中使用Elevator.evaluate()?可选:在Sightly中是否有类似于Elevatualator的东西可以避免使用pageContext?请看我问题的最后一句。所有空指针异常都是重复的…@AshleyMedway问题不是关于空指针异常,而是关于以不同方式使用特定方法。是的,提供的代码工作正常,它将我重定向到所需位置,但我仍然担心升降器的功能缺失。谢谢你的回复。