Search code examples
javaaemsling

Submit form to Servlet AEM


The Servlet code looks like below

@SlingServlet(
        methods = {"POST"},
        resourceTypes = {"cq:Page"},
        extensions = {"html"})
public class AssetDownloadServlet extends SlingAllMethodsServlet {

    private static final Logger log = LoggerFactory.getLogger(AssetDownloadServlet.class);


    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
    {
        log.info("%%%%%%%%%%%%%%$$$$$$$$$$$%%%%%%%**************:   "+ "doPost");
        processRequest(request,response);
    }

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
    {
        log.info("%%%%%%%%%%%%%%$$$$$$$$$$$%%%%%%%**************:   "+ "doGet");
        processRequest(request,response);
    }

    private void processRequest(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException{
        log.info("%%%%%%%%%%%%%%$$$$$$$$$$$%%%%%%%*******************");
        log.info(request.getParameter("format"));
        log.info("%%%%%%%%%%%%%%$$$$$$$$$$$%%%%%%%*******************");
    }
}

The form in html page looks like below

<form method="POST">
  <fieldset>
    <p>Format needed?</p>
    <ul style="list-style-type:none" data-sly-list.rendition="${renditions}">
      <!--${rendition.path}-->
      <li>
        <label>
          <input type="radio" name="format" value="${rendition.name}" />
          <span>${rendition.displayName}</span>
        </label>
      </li>
    </ul>
  </fieldset>
  <fieldset>
    <button type="submit">Start Download</button>
    <a>Cancel Download</a>
  </fieldset>
</form>

CQ Details on the Page

<!--cq{"decorated":false,"type":"myProject/components/page/generic","path":"/content/myProject/en/assetdetail/jcr:content","selectors":"IRNHUF7D","servlet":"Script /libs/foundation/components/page/page.jsp","totalTime":28,"selfTime":8}-->

Summary:

I have a cq:page resource, with a form and servlet linked to it. But on submitting the form the servlet is not betting initiated. I see that properties are set properly in http://localhost:4502/system/console/ .

Please suggest..

Thanks


Solution

  • cq:Page is not a resourceType, its a nodeType. You need to fix the resourceType in the servlet to your sling:resourceType in jcr:content node of the page. Another thing to note is that your servlet with a resourceType set will not be invoked if your path points to cq:Page node, it needs to point to jcr:content node to be picked up by the servlet.

    @SlingServlet(
            methods = {"POST"},
            resourceTypes = {"path/to/resource/type"},
            extensions = {"html"})
    public class AssetDownloadServlet extends SlingAllMethodsServlet {
    

    And your form definition should be like -

    <form method="POST" action="${currentPage.path}/_jcr_content.html"> 
    

    Note that jcr:content is written as _jcr_content