I have a Struts2 form that is included in a DIV overlay on a page. When I submit this form I want it to hide/show a hidden text area on the bottom of the form that just lets the user know that the form was submitted. From there the user could close the overlay and return back to their work. I’m having a problem accomplishing this. Upon my Return of a SUCCESS or NONE the page will redirect to a blank page if I do not provide it with another page in the return. How can I submit the form and stay within the same page without redirecting at all?
Currently I am submitting the form using JavaScript
document.forms['formName'].submit();
I am using JavaScript as opposed to the <s:submit/> option because there are other parameters that need to be set on the form itself prior to form submission.
What you need is: AJAX.
In Struts2 you can achieve it out-of-the-box with an
<sx:div>of the( deprecated since Struts 2.1, but still here and working) Struts2-Dojo Plugin.Basically, you target the div with the dojo topic, and the result of your Action, a JSP file containing an HTML/JSP snippet, will fill the div without reloading the page.
But if you are using the latest versions of Struts2, you can easily achieve this with Struts2-jQuery Plugin, and you can find all the working example with the code too in its Showcase.
This is an example:
Pretty easy 🙂
You can obviously alter the
hrefattribute withjavascripttoo, if you need to set parameters at runtime… just get id by id and alterhrefattribute.