如何在特定页面的servlet中创建WCMUsePojo实例?

时间:2016-06-02 04:01:59

标签: aem sling

我正在尝试在servlet中创建WCMUsePojo的实例。实现类已经用在了明亮的模板中。我已经尝试过下面的代码,无法让它工作。有任何想法吗?感谢。

    @Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {

    try {
        Resource     resource = request.getResource().getResourceResolver().getResource
                ("/content/mynewsite/homepage");
        WCMUsePojo template = resource.adaptTo(BaseTemplate.class);
        template.getPageManager(); // Does not work
    } finally {
        log.error("Error processing servlet");
    }
}

2 个答案:

答案 0 :(得分:0)

嗯,我不太确定我是否理解你的问题,但我希望有所帮助。 Sling Models可能是错误的方法,因为一些注入器需要脚本引擎来提供绑定,而Sling Models通常用于提供组件模型或装饰/包装OSGi服务。如果您需要更复杂的解决方案,则可能需要编写自己的Sling适配器。

package io.servlets;

import com.day.cq.wcm.api.NameConstants;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Template;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import java.io.IOException;

public class ContentServlet extends SlingSafeMethodsServlet {

    @Override protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response)
            throws ServletException, IOException {
        final Resource resource = request.getResource();
        final ResourceResolver resolver = resource.getResourceResolver();
        final PageManager pageManager = resolver.adaptTo(PageManager.class);
        final Resource contentResource = resolver.getResource("/content/mynewsite/homepage");
        if (null != pageManager && null != contentResource) {
            final Page page = pageManager.getContainingPage(contentResource);
            // process the content here
            //
            // not available on publish
            // final Template template = page.getTemplate();
            //
            // use
            final String templatePath = page.getProperties().get(NameConstants.NN_TEMPLATE, String.class);
            if (StringUtils.isEmpty(templatePath)) {
                // handle unexpected empty template path
            } else {
                final Template template = getTemplate(resolver, templatePath);
                // check for null - it is troublesome ... I know
            }
        }
    }

    private Template getTemplate(final ResourceResolver resolver, final String templatePath) {
        final Resource templateResource = resolver.getResource(templatePath);
        if (null == templateResource) {
            // handle unexpected missing template
            return null;
        } else {
            final Template template = templateResource.adaptTo(Template.class);
            if (null == template) {
                // handle broken adapter
                return null;
            } else {
                return template;
            }
        }
    }
}

答案 1 :(得分:0)

我有类似的problem。在我的测试中,我发现下面的模式似乎是实例化WCMUsePojo类。我不知道你的案件需要什么样的绑定。

BaseTemplate template = new BaseTemplate();
SimpleBindings bindings = new SimpleBindings();
bindings.put("resource", resource);
template.init(bindings);