AEM Developer Series
- Day 00: AEM Developer Series
- Day 01: Introduction to AEM
- Day 02: AEM Architecture
- Day 03: Setting up AEM Development Environment
- Day 04: Developing First OSGi Bundle
- Day 05: Working with Sling Servlets in AEM
- Day 06: Playing with Sling Post Servlet
- Day 07: Creating your first component in AEM
- Day 09: Dueling with Java Use API
- Day 10: Getting to know Sling Models
- Day 11: Client Libraries in Action
- Day 12: Creating your custom OSGi Configuration
- Day 13: Schedulers in AEM
- Day 14: Eventing in AEM
- Day 15: Custom Workflows in AEM
- Day 16: Creating JMX Beans in AEM
- Day 17: Working with QueryBuilder API
- Day 18: Working with Granite Datasources in AEM
- Day 19: Replication API in Action
- Day 20: Working with Users and Groups in AEM
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?
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 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.
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.
Happy Coding 😊