For AI agents: the complete documentation index is available at https://nhanbe.id.vn/llms.txt, the full documentation bundle is available at https://nhanbe.id.vn/llms-full.txt, and this page is available as Markdown at https://nhanbe.id.vn/aem/foundation/your-first-component.md.
#Component Structure and Building Your First Component in AEM
Components are the primary building blocks of AEM pages. A button, a text block, a carousel, or a hero banner — each of these elements is a component. In this article, we will build a component from scratch and understand every file involved.
Every complete AEM Component is typically made up of three main parts (the triad). Not every component requires all three (an extremely simple component might only need HTL), but most real-world components use this architecture:
Component
File Type
Purpose
Dialog
XML (.content.xml)
Defines the Authoring UI — input form fields for content authors.
HTL Template
.html
Responsible for rendering HTML markup based on data provided by the Model.
Sling Model
.java
Reads content data from JCR, processes business logic, and safely supplies data to the Template.
The Sling Model reads values from the JCR node and prepares data for HTL:
package com.mysite.core.models;import org.apache.sling.api.resource.Resource;import org.apache.sling.models.annotations.Default;import org.apache.sling.models.annotations.DefaultInjectionStrategy;import org.apache.sling.models.annotations.Model;import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)public class HelloModel { @ValueMapValue @Default(values = "Hello, World!") private String greeting; @ValueMapValue @Default(values = "This is my first AEM component.") private String message; public String getGreeting() { return greeting; } public String getMessage() { return message; }}
Annotation Explanations:
Annotation
Meaning
@Model(adaptables = Resource.class)
Declares this class as a Sling Model adapted from a Resource (JCR Node).
@ValueMapValue
Injects values from the component's JCR properties into this variable.
@Default
Provides a default value if the Author hasn't entered anything yet.
Important: If you omit defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, Sling Model's default strategy is REQUIRED. If an author drags the component onto a page without entering form data (no JCR data yet), the model fails and throws an error instead of displaying defaults. Always use OPTIONAL.
Note: The name attribute (such as name="./greeting") is critical. It defines the property name saved in JCR and must match the variable injected in your Sling Model (private String greeting;).
In real project scenarios, instead of building basic components like "Text" or "Image" from scratch, we use Core Components (provided out-of-the-box by Adobe) via the Proxy Component design pattern.
Create a lightweight component pointing to a Core Component via sling:resourceSuperType:
Thanks to sling:resourceSuperType, your component inherits all features, dialogs, and Sling Models from core/wcm/components/text/v2/text. You only override specific files (such as HTL) if you want custom markup. The Maven archetype generates these proxies automatically when initializing a project.