I'm working with struts2 and I have a form and I want to get all values in a JSON array.
To get this JSON array I made a JavaScript snippet to convert the form in JSON and send it to a Struts action during the submit event:
<script type="text/javascript">
jQuery("#advanced-search").submit(function(e){
var formInput=jQuery(this).serializeJSON({parseBooleans: true,parseNulls: true});
jQuery.ajax({
type: "POST",
url: "<s:url action='searchJSON'/>",
data: "jsonForm="+JSON.stringify(formInput),
dataType: "json"
});
});
</script>
My form action is implemented like :
<form action="javascript:void(0);" id="advanced-search" method="post">
...
</form>
The action is called and I can use my JSON array. The next step is to display the result to another page like search => result page. So the result of my action is a tiles (jsp):
<action name="search" class="searchAction" method="search">
<result name="success" type="tiles">results</result>
<result name="error" type="tiles">error</result>
</action>
Java code:
public String search() {
JSONObject jObj = (JSONObject) new JSONTokener(jsonForm).nextValue();
....
return SUCCESS;
}
The problem is here... my form is submitted and my action is called with a good JSON array, but the success result doesn't display the jsp.. in the browser, the page is still the form.
Anyone has an idea of why the redirect to the result page is not done ?
Is it a good way to have the form submitted as a JSON array?