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


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


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


I have a similar <a href="https://stackoverflow.com/questions/38212997/how-to-create-instance-of-wcmusepojo-in-my-prosper-spec" rel="nofollow">problem</a>. 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);


  • NULLIF in SQL Loader to compare a string value
  • CQ5 OSGi bundle does not start:- Activator cannot be found
  • Is there a way to specify parameters for included methods in TestNG suite.xml?
  • Android convert JSONObject to HashMap and display in ListView with SimpleAdapter
  • Failed to run analysis SonarQube 5.2 from Jenkins with specific artifact name
  • Passing data from activity to fragment transaction.replace()
  • Extract data from Specific format CSV files
  • Which Testing framework will suit for Adobe CQ5 Project?
  • Which config is applied when number of matched run modes is the same
  • Display JSON returned from Flask in a neat way
  • adding to a jruby class's getDeclaredMethods()
  • Display hashmap values in HTML dropdown box [closed]
  • AEM 6.3 - ResourceResolverFactory is null in Service and throwing LoginException in Sling Model clas
  • draw multiple encoded polylines using api v3
  • DotNet version of Windows Scripting Host
  • How do you copy a MS SQL 2000 database programmatically using C#?
  • Grid creating extra spacing that I don't want
  • Getting IIS6 to play nice with WordPress Pretty Permalinks
  • Update varbinary(MAX) field in SQLServer 2012 Lost Last 4 bits
  • Django model for a Postgres view
  • Combining two different ActiveRecord collections into one
  • Security issues with PHP's Readfile method
  • How to make jdk.nashorn.api.scripting.JSObject visible in plugin [duplicate]
  • rspec simple example getting error on request variable in integration test
  • Stop Bash Script if Hive Fails
  • Create DicomImage from scratch using Dcmtk
  • How to attach a node.js readable stream to a Sendgrid email?
  • Email verification using google app script and google forms
  • Meteor: Do Something On Email Verification Confirmation
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • How to do unit test for HttpContext.Current.Server.MapPath
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Spray.io: When (not) to use non-blocking route handling?
  • Volusion's generic SQL folder, functionality
  • Where to put my custom functions in Wordpress?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • RestKit - RKRequestDelegate does not exist
  • KeystoneJS: Relationships in Admin UI not updating
  • WPF Applying a trigger on binding failure
  • How does Linux kernel interrupt the application?