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.

Example

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 TextComponent2.java 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 TextComponent2Model.java and paste the following code in it.
  • Let's look at the class TextComponent2.java 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 (TextComponent2.java), 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.

Conclusion

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 😊



Comments

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 01: Introduction to AEM

Day 04: Developing first OSGi bundle