Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

Core Form Custom Action Type Calling a Servlet instead of a Service

Avatar

Level 2

Hi,

I have successfully added a custom action in the dropdown of core form component. 

edeluyas_0-1668750378637.png

Before I have a post.POST.jsp on my custom action to call a service to process the input data on my form but I have a new required to use a servlet instead. I have seen this thread on how to call a servlet. I already have a core form that extends to

core/wcm/components/form/container/v2/container

And made some changes to the action attribute to add the selector on the url. Here's the code of my container.html

<form data-sly-use.container="com.adobe.cq.wcm.core.components.models.form.Container"
data-sly-use.grid="com.day.cq.wcm.foundation.model.responsivegrid.ResponsiveGrid"
method="POST" action="${container.action @selectors='formsubmission'}" id="${container.id}" name="${container.name}"
enctype="${container.enctype}"
class="cmp-form ${grid.cssClass}" novalidate>
<div data-sly-test="${container.errorMessages}"
data-sly-list.item="${container.errorMessages}"
class="cmp-form-error">
<p class="cmp-form-error__item">${item}</p>
</div>
<input type="hidden" name=":formstart" value="${resource.path}"/>
<input type="hidden" name="_charset_" value="utf-8"/>
<input type="hidden" id="form-hidden-thankyou" name="thankYouPageUrl" value="${container.redirect @ extension='html'}">
<input type="hidden" id="form-hidden-error" name="errorPageUrl" value="${container.errorPageUrl @ extension='html'}">
<input data-sly-test="${container.redirect}" type="hidden" name=":redirect" value="${container.redirect @ extension='html'}"/>
<sly data-sly-repeat.paragraph="${grid.paragraphs}"
data-sly-resource="${paragraph.path @ resourceType=paragraph.resourceType, decorationTagName='div', cssClassName=paragraph.cssClass}"></sly>
<sly data-sly-resource ="${resource.path @ resourceType=container.resourceTypeForDropArea, appendPath='/*', decorationTagName='div', cssClassName='new section aem-Grid-newComponent'}"
data-sly-test="${wcmmode.edit || wcmmode.preview}"></sly>
</form>

 Then the code snippet of my servlet.

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

@Component(service = Servlet.class, property = {
Constants.SERVICE_DESCRIPTION + "=Servlet for Custom Form Submission",
"sling.servlet.methods=" + HttpConstants.METHOD_POST,
"sling.servlet.resourceTypes=" + FormSubmissionServlet.SERVLET_RESOURCE_TYPE,
"sling.servlet.selectors=" + "formsubmission",
"sling.servlet.extensions=" + "html"
})
public class FormSubmissionServlet extends SlingAllMethodsServlet {

static final String SERVLET_RESOURCE_TYPE = "sling/servlet/default";

@Override
protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException {
// code logic here
response.sendRedirect("/content/el/au/en_au/ax-ootb-components/forms/thank-you.html");
}
}

 But whenever I try submitting my form it seems like the servlet is not being called. Can't see anything on the console or network tab on the dev tools to give me at least a hint what's happening. I've checked the access.log and error.log but can't find any related error. So currently it's not working. Any idea what else can I check or what might be wrong?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @edeluyas 

 

If its still an issue, could you please inpect page and check what is the value on the action attribute of form.

 

Thanks,

Nupur

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

Hi @edeluyas 

 

If its still an issue, could you please inpect page and check what is the value on the action attribute of form.

 

Thanks,

Nupur