Skip to main content

Day 09: Dueling with Java Use API

AEM Developer Series

We discussed JavaScript Use API and coded server-side logic using JavaScript in the previous post. Today, we are going to look into a more popular way of dealing with server-side logic using Java Use API.
We will be creating a simple text component (similar to the one in the previous post) and will use Java backend logic to do our task.

What on earth is WCMUsePojo?

You must have heard about WCMUsePojo term while developing with AEM. What is it? Actually, it is a class that enables us to talk to the front end of our component. It is similar to the JavaScript Use API in usage. 

Since we likely need a way to provide back-end logic to components in AEM’s component development mechanism, we write Use API logic

We also know that HTL is a limited template language that allows doing only small basic operations, and the heavy lifting logic should be done inside a Java class or a server-side JS.

This provides better decoupling of the business logic and so your code will be more easily maintainable and also easier to debug. All of this is achieved by extending WCMUsePojo in our custom class which provides us with the capability to deal with JCR.


After understanding the purpose and usage of the API, it's time to see the concepts in action 😮.
  • Create a new AEM project using AEM Multimodule Project (how? see here) and deploy in your AEM instance.
  • Go to CRXDE and under your project in /apps create a new component with the following configuration
Create a component
  • After creating the component node, create a cq:dialog underneath and configure it as follows -
  • Now go to the eclipse in the core child project and create a class named and paste the following code in it
  • This class requires a model class to save the values in memory. For this, I created one simple model class with two properties - title and description (we mimicked the edit dialog properties). Now, create a model class and paste the following code in it.
  • Let's look at the class and understand its code. First, we have extended the class WCMsePojo to make use of Java Use API
  • Now, in the overridden activate() method, we will write the logic. We are getting the adapted Node object from the getResource() method which is available due to the use of WCMUsePojo class. We then are checking the existence and getting the properties from the JCR using javax.jcr.Node API. After getting the properties, we are setting them in the model object.
  • Now, rename text2.jsp to text2.html and paste the following code in it
  • Here, we are using data-sly-use to include the Java file (, then we are accessing the values via the model object.
  • Now drag and drop the component on the page and configure it using the edit dialog. You will see the passed values on your page.


Congratulations!! 🙋 you have duelled with Java Use API which can be used to write server-side logic. I hope you enjoyed this post.

You can find the complete code of this project on my GitHub. Feel free to fork or open issues, if any.

I would love to hear your thoughts on this and would like to have suggestions from you to make it better. 

Feel free to befriend me on Facebook, Twitter or Linked In or say Hi by email.

Happy Coding 😊


  1. I get this error " Cannot get DefaultSlingScript: Compilation errors in org/apache/sling/scripting/sightly/apps/demoproject/components/content/text2/
    Line 38, column 1724 : org.redquark.demo.core.cqcomponents.TextComponent2 cannot be resolved to a type"

    Whatever I tried to do, I couldn't manage to make it work. I think that the backend doesn't compile at all. In the pom.xml file there is no all. It seems that, in other projectS the backend is included in the file in the package manager. How do you build and install it?
    Thank you!

    1. Hi,
      As it is evident from the message, your TextComponent2 class is not being resolved at all. Could you please share your code so that we can see what is going on?
      You can take reference from the code repository -

    2. It is the same as yours. I believe that the problem is on the compiler. I make changes on the and I don't see any change. It seems that the backend is not compiled at all
      I uploaded the cq:dialog here .

      package org.redquark.demo.core.cqcomponents;

      import javax.jcr.Node;

      import org.redquark.demo.core.models.TextComponent2Model;
      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;

      import com.adobe.cq.sightly.WCMUsePojo;

      public class TextComponent2 extends WCMUsePojo {

      private static final Logger log = LoggerFactory.getLogger(TextComponent2.class);
      private TextComponent2Model model;

      public void activate() throws Exception {
      try {"Text Component 2.0 backend logic starts");
      model = new TextComponent2Model();
      Node node = getResource().adaptTo(Node.class);
      if(node.hasProperty("title")) {
      String title = node.getProperty("title").getString();

      if(node.hasProperty("description")) {
      String description = node.getProperty("description").getString();

      } catch (Exception e) {
      log.error(e.getMessage(), e);

      public TextComponent2Model getModel() {
      return model;

    3. Hi, can you please check if your bundle is in the "Active" state or not. If it is in the active state then just upload the bundle directly via ./system/console/bundles.
      I am assuming here that when you run "mvn clean install", it completes successfully. Try "clean and rebuild" the project also.

    4. Well, after a lot of try and error, the only thing that seemed to work was "mvn -PautoInstallBundle clean install" Inside the core folder. mvn clean install on the parent folder didn't give any errors but wouldn't accept the changes.

    5. I don't know the reason of this but the command "mvn -autoInstallPackage clean install" in the root folder doesn't work in 100% of the cases. That is why I suggested to directly install the bundle via ./system/console/bundles.


Post a Comment

Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.
Adobe is way ahead in this war and has gained a lot of market capture. They are leaders in the Digital Marketing platforms since 2014-15. One of the flagship product in Adobe's Digital Marketing suite is Adobe Experience Manager (AEM).
Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.
When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the res…

Day 05: Working with Sling Servlets in AEM

Day 03: Setting up AEM Development Environment