Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Generation of nodes based on the values entered in the dialog of a component ??

Avatar

Level 2

Hi All, 

I have a parent component at /apps/project/component/content/parentComponent . with the help of this component . I am selecting one or more of 3 different components(we can call them children components). 

Generally what happens is that when i open and configure the dialog of this parentComponent, a node of name parentComponent gets created in the CRXDE. but since I am specifying all my children components also in this same dialog , I want the nodes of those children components also to be created below the parentComponent after OK button is clicked.

My thought process is to override the default servlet which gets called on click of OK button.. 

I have tried to create my own custom servlet but somehow it is not getting called.

Below is my custom servlet skeleton:


@Component(metatype = false)
@SlingServlet(resourceTypes = "apps/project/components/content/parentComponent /parentComponent ",extensions = "html", methods = {"POST"}, generateComponent = false)
public class MyServlet extends SlingAllMethodsServlet {

    private final static org.slf4j.Logger logger = LoggerFactory.getLogger(MyServlet .class);

    @Override
    protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
            throws ServletException, IOException {
        logger.debug("MyServlet ::doPost()");
        System.out.println("POST!!");
        response.getWriter().write("Hello MyServlet World!");
    }


}

 

Am I missing sth ?? 

Pls help me with this.

 

Thanks

~Ashish

1 Accepted Solution

Avatar

Correct answer by
Level 10

See this community article -- it may help you -

http://www.citytechinc.com/us/en/blog/2012/04/complex_cq5_dialog_validation.html

It talks about using a custom servlet from a CQ dialog. (it may help you in calling your servlet)

View solution in original post

3 Replies

Avatar

Level 10

YOu are on the right track. However -- if you want to call Java from a JSP (which is really what an AEM component is), instead of using a Servlet -- setup an OSGI bundle and call the OSGi bundle via sling.getService().

For example:
//This is a component so it can provide or consume services

@Component
  
@Service
public class NodeServiceImpl implements NodeService {
  

//method that uses the jcr api
public int  createNode()

{

       ... 
      //USE JCR API HERE TO CREATE NODES AND PERFORM OTHER JCR operations;

        return 0;

}

From your JSP -- you can use sling to get an instance of NodeService and invoke createNode:

<%@include file= "/libs/foundation/global.jsp" %>
<%@ page import= "org.apache.sling.commons.json.io.*,com.adobe.cq.*" %><%
S
  
com.adobe.cq.NodeService cs = sling.getService(com.adobe.cq.NodeService .class);
  
cs.createNode() ;
   
%>

 

To see an article that puts this all together and uses the JCR API from within an OSGi bundle -- see:

http://helpx.adobe.com/experience-manager/using/querying-experience-manager-data-using1.html

 

Hope this helps

Avatar

Level 2

Hi Smacdonald,

Thanks for your reply.

I implemented the functionality with the help of Post Processor. Below are he steps to reproduce my method:


/**
 * Processes a POST request and sends appropriate response by creating child nodes
 */
@Service
@Component(metatype = true,immediate = true)
public class ParentPostProcessor implements SlingPostProcessor {
  //variables come here

    /**
     * processes the post request
     * @param request the request Object
     * @param changes the list of Modified properties.
     */
    public void process(SlingHttpServletRequest request, List<Modification> changes) {

    // This PostProcessor should only act on resources with "resourceType path"
        //and if the operation is not delete
        if (request.getResource().getResourceType().equals("mysite/components/myparent") 
            && !"delete".equals(request.getParameter(":operation")) {
            
            createChildNodes(request);
        }
    }

}

Avatar

Correct answer by
Level 10

See this community article -- it may help you -

http://www.citytechinc.com/us/en/blog/2012/04/complex_cq5_dialog_validation.html

It talks about using a custom servlet from a CQ dialog. (it may help you in calling your servlet)