1 Reply Latest reply: Jul 27, 2013 6:56 PM by msulliva RSS

    Handling Post requests to update node content

    krisgumm Community Member

      Hi ,

       

        I'm pretty new to CQ5 and trying to handle HTTP post request with my component .  My directory structure is

       

      /apps/training/components/page/trainingpage/trainingpage.jsp

        /apps/training/components/page/trainingpage/trainingpage.POST.jsp

       

      In my trainingpage.jsp ,

       

      i have following

       

      <form action="<%currentNode.getPath();%>.html" method="POST" enctype="multipart/form-data">

          <input type="text" name="name" /><br />

          <input type="text" name="address" /><br />

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

      </form>   

      </html>

       

      When ever i submit this form  , i receive status 200 message with following and the properties gets appended at the root 'content ' and not at 'content\trainingsite'

       

      Status

      200

      Message

      OK

      Location/content
      Parent Location/
      Path

      /content

      Refererhttp://localhost:4502/content/Trainingsite.html
      ChangeLog

      <pre>modified("/content/name");<br/></pre>

       

      My assumtion is that the action url to my custom jsp is not being resolved and the default post servelt might be getting called which is updating the properties at the root content in the repository . I have even tried this case with updating my custom jsp file name from trainingpage.POST.jsp to POST.jsp but it's still the same .

       

      The node path and resource path are returning as expected                             Node path :          /content/Trainingsite/jcr:content                    

                                                                                                                                         Resource path :     /content/Trainingsite/jcr:content

       

      Any pointers for any missing links ? Thanks for your help .

       

      cheers


        • 1. Re: Handling Post requests to update node content
          msulliva Community Member

          This should work, perhaps it's that <%currentNode.getPath();%> doesn't output anything in your form action (to output use <%= property %> without the semicolon instead of the scriptlet <% %> which just evaluates the contents instead of outputing), it seems CQ inserts a parent there in the action for some reason, in your case /content, so your form is posting to /content which isn't where your POST.jsp is located.  The path you really probably want is to post directly to your component, which would look something like /content/Trainingsite/_jcr_content.html which would be output by <%= currentNode.getPath() %>.html, I used the ${currentNode.path} / ${currentPage.path} EL syntax in my example because I think it's cleaner looking and avoids the scriptlet confusion.

           

          That said, why not just use the Sling Post Servlet [1] which will save the contents of the form and use the redirect option to avoid the standard Sling output (note the ./ prefix to the property names, this tells the post servlet to store that value at wherever your form action is posting to):

           

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

          <%@page session="false"%>

          <h1>Hello ${properties.name}</h1>

          <p>From: ${properties.address}</p>

          <form action="${currentNode.path}.html" method="POST" enctype="multipart/form-data">

              <input type="text" name="./name" value="${properties.name}" placeholder="Name" /><br />

              <input type="text" name="./address" value="${properties.address}" placeholder="Address" /><br />

              <input type="hidden" name=":redirect" value="${currentPage.path}.html" />

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

          </form>

           

          [1] http://sling.apache.org/site/manipulating-content-the-slingpostservlet-servletspost.html#M anipulatingContent-TheSlingPostServlet%2528servlets.post%2529-%257B%257B%253Aredirect%257D %257Dhttp://sling.apache.org/site/manipulating-content-the-slingpostservlet-servletspost.html