重定向实施

时间:2015-02-26 16:29:30

标签: 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或类似的东西?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

  • 阅读该物业
  • 验证并外化
  • response.sendRedirect是(链接)