display saved properties of a node in a form on page load | Community
Skip to main content
Level 3
October 16, 2015
Solved

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

  • October 16, 2015
  • 3 replies
  • 1363 views

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 .

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Ojjis

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

3 replies

Ojjis
Level 7
October 16, 2015

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

Level 4
October 16, 2015

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

Ojjis
OjjisAccepted solution
Level 7
October 16, 2015

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