2 Replies Latest reply on Apr 2, 2013 7:49 AM by RossRaphael

    Syntax for a form posting to a post handler

    RossRaphael

      So I have a component with a component JSP that contains :

       

      <form id="flagMessage" name="flagMessage" method="POST" action="/post.POST.html">

         <input type="textarea" name="reason" rows="4" cols="50"/><br>

         <input type="submit" value="Submit"/>

         <input type="button" value="Cancel" onclick="$CQ('#<%=id%>-flag-form').toggle()"/>

      </form>

       

      then I have a post handler in the component at /actions/post.POST.jsp

       

      I end up getting a 500 where it complains about the property reason, referring to my textarea. A log statement in my handler never gets written to the log. I haven't had to do something like this in a long time and I know its something small that I am missing, but I couldn't put my finger on it.

        • 1. Re: Syntax for a form posting to a post handler
          pchamoun

          The following example shows how to do a post and a get. You can use this example as a base for any simple tool you want to implement in CQ.

           

          Here is how I would do it:

          1- Create a node (/apps/tools/components/FormPosting) of type sling:Folder

           

          2- Create an html.jsp under FormPosting and type the following code in it:

          <%@include file="/libs/foundation/global.jsp"%>

          <sling:defineObjects /><html><head><title>Form Posting Example</title></head><body>

          <cq:include script="form.jsp" />

          </body>

          </html>

           

          3- Create an form.jsp under FormPosting and type the following code in it:

          <%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>

          <%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0"%>

          <cq:defineObjects />

          <%@include file="/libs/foundation/global.jsp"%>

          <html>

          <head>

          <title>Form Posting Example</title>

          <meta http-equiv="Content-Type" content="text/html; utf-8" />

          <script src="/libs/cq/ui/resources/cq-ui.js" type="text/javascript"></script>

          <script type="text/javascript">

              function submitform(){

                  var paths = document.formposting.pathList.value;

                  if(paths.length == 0){

                      alert("List can not be empty");

                      return;

                  }

                  document.formposting.submit();

              }

              </script>

          </head>

          <br />

          <form name="formposting" method="post"></br>

          Enter list of paths (Here please don't give base path)<b>

          <font color="red">*</font></b>: </br>

          <TEXTAREA NAME="pathList" id="pathList" ROWS="5" COLS="50"></TEXTAREA> <br></br>

          Name: <input type="text" name="name" id="name">MyPackage.zip</input></br>

          Group: <input type="text" name="someGroup" id="group">MyPackageGroup</input></br>

          <input type="button" value="Post" onclick="javascript:submitform()" />

          </form>

          </html>

           

          4- Create an html.jsp under FormPosting and type the following code in it:

          <%@page session="false" import="javax.jcr.*,

                  com.day.cq.wcm.api.Page,

                  com.day.cq.wcm.api.PageManager,

                  org.apache.sling.api.resource.Resource,

                  java.io.PrintWriter,

                  javax.servlet.jsp.JspWriter" %>

                 

          <%@include file="/libs/foundation/global.jsp"%>

          <sling:defineObjects />

          <%@page import="org.osgi.framework.BundleContext"%>

           

           

          <html>

          <head><title>Form Positing Example</title></head>

          <body>

          <cq:include script="form.jsp" />

          <%

          Session sess = resourceResolver.adaptTo(Session.class);

          BundleContext bundleContext = sling.getService(BundleContext.class);

          // Get stuff from the bundleContext

           

           

          String pathList = request.getParameter("pathList");

          String name = request.getParameter("name");

          String someGroup= request.getParameter("someGroup");

          String[] paths = pathList.split(",");

           

           

          out.println("<br> Adding following path:");

          for(int i=0;i<paths.length;i++) {

               out.println("<br><font color=red>"+paths[i]+"</font>");

          }

           

           

          String serverName = (request.getProtocol().split("/")[0]).toLowerCase()+"://"+request.getServerName();

          if(!"".equals(request.getServerPort())){

              serverName+=":"+request.getServerPort();

          }

           

           

          String docroot = serverName +"/crx/packmgr/service.jsp";

          out.println("<br><br><strong>Name is: "+ name+"</strong>");

          out.println("<br><br><strong>Group is: "+ name+"</strong>");

          %>

          <form method="get" action="<%=docroot %>">

          <input type="hidden" name="cmd" id="cmd" value="get"></input>

          <input type="hidden" name="name" id="name" value="<%=Text.escape(name) %>"></input>

          <input type="hidden" name="group" id="group" value="<%=Text.escape(someGroup) %>"></input>

          <input type="submit" value="Download Package"></input>

          </form>

          </body>

          </html>

           

          5- Create the following nt:unstructured node (/apps/tools/components/FormPosting/run) with sling resourceType = /apps/tools/components/FormPosting

           

          6- To test go to (http://localhost:4502/apps/tools/components/FormPosting/run.html). If you have a package in some group then type the name and the group it blongs to and then do the test.

          1 person found this helpful
          • 2. Re: Syntax for a form posting to a post handler
            RossRaphael Level 1

            Here is the outcome I came up with

             

            <form id="flagMessage" name="flagMessage" method="POST" action="<%= resource.getPath() %>.html>

             

            And I placed POST.jsp in the same directory adjacent to my component JSP, which has worked. 

            1 person found this helpful