Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

display saved properties of a node in a form on page load

Avatar

Level 3

Hey there ,

 I'm new to CQ and playing around with forms .  I have a html form (not cq form) where i am posting to a custom servlet which creates properties on a specific node in jcr  and also saves the values to a .txt file  based on some custom logic  .

After successful post ,  i want to return to the same form page with saved values reloaded on the form ? Normally my requirement is  , only to write to a .txt file on post , but since i have to show the saved values to user reloaded in a form I'm also saving to node so that they can be easily retrieved when compared to parsing the text file .

What would the best way to achieve it ?  

Thanks in advance for your pointers and tips .

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi, well in that case. If you have your values stored under a node you can retrieve them with something 
like thisin the jsp page:

//.. in jsp page //..code String MY_PATH = "/path/to/the/specific/node"; Resource myNodeResource = resourceResolver.getResource(MY_PATH); ValueMap myValuemap = myNodeResource.adaptTo(ValueMap.class); //...more code fetch the values of interes and check that they exist

.. then get the values from the value map and then just init the form with these values.
Of course you need some better code than the above, with error handlig etc. but you get the basic idea.

Good luck
/Johan

View solution in original post

3 Replies

Avatar

Level 7

On easy way to accomplish this would be to make the servlet redirect back to the same form with the submitted values as variables (in the way you choose). An example of this could be 

//In the servlet //...lots of code.. session.setAttribute("email", email == null ? "" : email); session.setAttribute("name", name == null ? "" : name); session.setAttribute("message", message == null ? "" : message); res.sendRedirect(req.getHeader("referer")+"#"+formId); return; //... more code

After this, you can simply retrieve those values in the jsp when the page loads and do something with them.

/Johan

Avatar

Level 4

Hi Johan ,

Thanks for the reply. The way you mentioned would work for case in which immediately after successful post,load the form  with the values in session.

 How about the case in which I freshly open the form page (state of the form page before i do a post)and it loads with saved properties, 

 

thanks in advance

Avatar

Correct answer by
Level 7

Hi, well in that case. If you have your values stored under a node you can retrieve them with something 
like thisin the jsp page:

//.. in jsp page //..code String MY_PATH = "/path/to/the/specific/node"; Resource myNodeResource = resourceResolver.getResource(MY_PATH); ValueMap myValuemap = myNodeResource.adaptTo(ValueMap.class); //...more code fetch the values of interes and check that they exist

.. then get the values from the value map and then just init the form with these values.
Of course you need some better code than the above, with error handlig etc. but you get the basic idea.

Good luck
/Johan