Menu

How to create a node or drop component in AEM using java program?

A simple and easy java code to create/add a node or drop a component in AEM.

In this tutorial, we are dropping an AEM component on AEM page (in a parsys or under a specific node) programmatically. To complete this requirement, we have to write code as follows:

First, we have to create an object of ResourceResolver either using ResourceResolverFactory or using session. In this example, we use ResourceResolverFactory interface service API to create the object of ResourceResolver to get the resource from AEM. (code snippet line # 6)
Now we will get the resource using ResourceResolver and adapt that resource in a JCR Node type, so we can deal with that resource and perform manipulation. ResourceResolver has a method getResource resource which makes AME resource available for manipulation. getResource() method required a relative to as parameter to get that resource. (code snippet line # 7)
Then using JCR API we will add a node under that resource/Node with its name(component name) and type(component type). (code snippet line # 8)
After that, we will set all the required property for that component. (code snippet line # 9-13)

Below is the complete code to drop a component in AEM using java. If you have any doubt please comment your doubt and questions in the comment section.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
@Reference
private ResourceResolverFactory resolverFactory;
ResourceResolver resolver;
private void addNodeOrComponentInAEM() {
 try {
  resolver = resolverFactory.getServiceResourceResolver(null);
  Node jcrContent=resolver.getResource("/content/we-retail/ca/en/men/jcr:content").adaptTo(Node.class);
  Node nextbuttonNode=jcrContent.addNode("nextButton","nt:unstructured");
  nextbuttonNode.setProperty("buttonType", "submit");
  nextbuttonNode.setProperty("componentName", "coralButton");
  nextbuttonNode.setProperty("fieldId", "submit");
  nextbuttonNode.setProperty("fieldLabel", "Next");
  nextbuttonNode.setProperty("sling:resourceType", "granite/ui/components/coral/foundation/button");
  
 } catch (Exception e) {
  
 } 
}

Pass by reference and pass by value in Java

Today in this tutorial of Java program we will see how to work with pass by value and pass by reference. 

Below is code snippet.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

package com.rashid.jorvee;


public class ValueRef {

 public static void main(String[] args) {

  Employee emp=new Employee();

  ValueRef valref=new ValueRef();

  emp.setSalary(12000.00);

  System.out.println("Employee emp salary: " +emp.getSalary());

  valref.salary(emp);

 }

 public void salary(Employee em){

  em.setSalary(14000.00);

  em=new Employee();

  em.setSalary(15000.0);

  System.out.println("Employee em salary: " +em.getSalary());

 }

}



class Employee{

 double salary;

 public double getSalary() {

  return salary;

 }


 public void setSalary(double salary) {

  this.salary = salary;

 }
}

Output:
Employee emp salary: 12000.0
Employee em salary: 15000.0