October 13, 2020
Estimated Post Reading Time ~

Call any servlet in touchUI dialog - AEM 6.3

Firstly we will write a dialog listener, in that listener we will send the request to a servlet and also we can send the values to the servlet using the data attribute.

Step 1: jquery code snippet:

(function(document, $, ns) {
"use strict";

$(document).on("click", ".cq-dialog-submit", function(e) {

$.ajax({
type: 'GET',
url: '/bin/sling/idvalidate',
data: {
text: "TEXT",
heading: "HEADING"
},

success: function(msg) {

alert('success');
}
});
});

})(document, Granite.$, Granite.author);


Step 2: Now in our Java servlet we will get the values of which were sent from jquery and perform any kind of operations and send back the response:

package com.ftd.platform.core.servlets;

import java.io.IOException;
import java.net.URL;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component(
service = { Servlet.class }, property = { " service.description=Servlet", " service.vendor=Adobe",
" process.label=Id Validator Service", "sling.servlet.paths=/bin/sling/idvalidate",
"sling.servlet.methods=GET" })

public class IdValidator extends SlingAllMethodsServlet {
private static final long serialVersionUID = -293773910749832945L;

String text;
String heading;
private static final Logger LOG = LoggerFactory.getLogger(IdValidator.class);

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

response.setContentType("application/json");
text = request.getParameter("text");
heading = request.getParameter("heading").toString();
response.getWriter().write(heading.toString());

}
}



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.