How can I create an instance of WCMUsePojo in a servlet for a specific page?

我与影子孤独终老i 提交于 2019-12-23 03:26:20

问题


I am trying to create an instance of WCMUsePojo in a servlet. The implementing class is already used in sightly templates. I have tried code below and could not get it to work. Any ideas? Thanks.

    @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");
    }
}

回答1:


Hm, I'm not quite sure if I understood you problem correctly, but I hope that helps. Sling Models may be the wrong approach here as some injectors require the scripting engine to provide the bindings and Sling Models are generally used to provide a component model or decorate/wrap OSGi services. If you need a more sophisticated solution you might have to write your own Sling adapter.

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;
            }
        }
    }
}



回答2:


I have a similar problem. in my tests I found the pattern below seems to instantiate WCMUsePojo classes. I don't know what bindings are needed in your case.

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


来源:https://stackoverflow.com/questions/37582439/how-can-i-create-an-instance-of-wcmusepojo-in-a-servlet-for-a-specific-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!