Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Form post "blocked" in Adobe CQ 5.6.1

Avatar

Level 3

HI all,

Can you help on something that should be simple but is proving not to be? I am attempting to post a simple feedback form on an instance of CQ5.6.1 using javax.mail MimeMessage. It works when passing no custom form input - that is, it will generate the email and send it. However, when any form variables are injected into that email, the form seems to be referred to "standard?" form handler. Am I missing some basic CQ setting here? Do I need to "register" the form and its variables in order for CQ to accept it?

Apologies if this sounds basic/stupid but I'm struggling to a useful steer on this one.

1 Accepted Solution

Avatar

Correct answer by
Level 10

I have never experienced issues posting form fields from a JSP in AEM.

However - I typically use AJAX to post to a 2nd JSP - as shown here:

$('#submit').click(function() {
    var failure = function(err) {
         alert("Unable to retrive data "+err);
          
    };
      
    //Get the ZIP COde value to pass to the CQ Web Service
    var myZip = $('#mydropdown').val() ; 
      
    var url = location.pathname.replace(".html", "/_jcr_content.lookup.json") + "?zip="+myZip;
    
    $.ajax(url, {
        dataType: "text",
        success: function(rawData, status, xhr) {
            var data;
            try {
                data = $.parseJSON(rawData);
                  
                //Set the fields in the forum
                $('#city').val(data.city); 
                $('#state').val(data.state);
                $('#description').val(data.description);
                $('#wind').val(data.wind);
                $('#temp').val(data.temperature);
                $('#hum').val(data.humidity);
                } catch(err) {
                failure(err);
            }
        },
        error: function(xhr, status, err) {
            failure(err);
        } 
    });
  });

Keep app logic in the JSP thin and place your business logic in OSGi bundles. THis is the recommended way when using AEM.

View solution in original post

6 Replies

Avatar

Level 10

Why not just post form to a servlet, get the posted form fields, then pass to a OSGi method that puts the email together and emails. Are you using an Adaptive form in AEM 6 or a custom form you created in a JSP? 

Avatar

Level 10

No it's not necessary to post to a servlet as AEM supports some out of the box endpoints - but like in .NET (best control is when you post to a .NET class) - when posting in AEM- you have the best control when you post to a custom SLing Servlet.  Why - because you can define exactly what happens using Java code. 

For example:

 @Override
     protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServerException, IOException {
       
      try
      {
         //Get the submitted form data that is sent from the
              //CQ web page  
          String id = UUID.randomUUID().toString();
          String firstName = request.getParameter("firstName");
          String lastName = request.getParameter("lastName");
          String address = request.getParameter("address");
          String cat = request.getParameter("cat");
          String state = request.getParameter("state");
          String details = request.getParameter("details");
          String date = request.getParameter("date"); 
          String city = request.getParameter("city"); 

//call a method to email these values  

Read this article to learn how to post to an AEM Sling Servlet:
https://helpx.adobe.com/experience-manager/using/custom-sling-servlets.html


Now for email - you can email using this code:

//Inject a MessageGateway Service and send the message
messageGateway = messageGatewayService.getGateway(Email.class);
  
// Check the logs to see that messageGateway is not null
 messageGateway.send((Email) email);


Read this article to learn how to code with messageGateway.

https://helpx.adobe.com/experience-manager/using/creating-custom-aem-workflow-steps.html

You can still configure messageGateway and use it in an OSGi as talked about in this article (it does not need to be only used in a custom workflow step).

Or you can write your own AEM EMAIL service and define your own email class. For this use case -- see: 

https://helpx.adobe.com/experience-manager/using/creating-custom-cq-email-services.html

So you have a few choices how to implement this, I personally would use messageGateway and configure it - but if this is not to your liking - define a custom AEM email service. 

Hope this helps.

Avatar

Level 3

Thanks again Scott,

We're already running a custom core on this CQ instance and creating a new class is an option - it just creates a deployment headache that I want to avoid in the short term. Again, sorry if I sound like a broken record, but what is the mechanism that prevents posted variables from being used in a JSP when it's posted back to itself? I appreciate the general benefits of a class-based system but am also interested in why 1. this doesn't work "as is" and 2. what tangible benefit an OSGI bundle really adds to this specific example (to increase my understanding of CQ).

Avatar

Correct answer by
Level 10

I have never experienced issues posting form fields from a JSP in AEM.

However - I typically use AJAX to post to a 2nd JSP - as shown here:

$('#submit').click(function() {
    var failure = function(err) {
         alert("Unable to retrive data "+err);
          
    };
      
    //Get the ZIP COde value to pass to the CQ Web Service
    var myZip = $('#mydropdown').val() ; 
      
    var url = location.pathname.replace(".html", "/_jcr_content.lookup.json") + "?zip="+myZip;
    
    $.ajax(url, {
        dataType: "text",
        success: function(rawData, status, xhr) {
            var data;
            try {
                data = $.parseJSON(rawData);
                  
                //Set the fields in the forum
                $('#city').val(data.city); 
                $('#state').val(data.state);
                $('#description').val(data.description);
                $('#wind').val(data.wind);
                $('#temp').val(data.temperature);
                $('#hum').val(data.humidity);
                } catch(err) {
                failure(err);
            }
        },
        error: function(xhr, status, err) {
            failure(err);
        } 
    });
  });

Keep app logic in the JSP thin and place your business logic in OSGi bundles. THis is the recommended way when using AEM.

Avatar

Level 3

Thanks again, that's exactly how I've done it (only my JQuery call is $post).

So, I guess the reason it's not working may be a more server level restriction then. Totally understand the thin business logic comment - and I totally agree. TBH, this form is so simple, it feels OK in a JSP. I need to ask the server techs to comment I think.

Thanks again for your attentiveness and clear, concise answers.

Avatar

Level 10

Also - take a look at this article:

https://helpx.adobe.com/experience-manager/using/using-jsonwriter-objects-display-cq.html

Its really the model i prefer when working with front end/back end. Front end collects and display data and back end contains the business logic. 

Also - you should never have issues when following this model.