1

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?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
malak
  • 27
  • 7
  • http://stackoverflow.com/a/1534662/1654265 – Andrea Ligios Nov 06 '14 at 13:40
  • It is not JSON was submitted. – Roman C Nov 06 '14 at 13:49
  • Because it's an Ajax post. Everything is sideband. If you want something to happen after the Ajax post you need to update your JavaScript to do something after a successful response (or a failure response). – Dave Newton Nov 06 '14 at 13:53
  • yes i know, but i though Struts could redirect the page despite the ajax call, is it possible ? – malak Nov 06 '14 at 15:12
  • No, you could even request an HTML page using ajax call, if you did so and the page was redirected you would get a different page... but that data is still being intercepted by your function. So you need to determine where to go based on the data you receive in your function. – Quaternion Nov 07 '14 at 22:01
  • ok thanks men, i found a solution and i will write it here after i complete it. – malak Nov 12 '14 at 14:44

1 Answers1

0

Ok after some tests... and with the comments (thanks guys) i wrote a solution.

I still use an ajax submission with a JSON format, but the callback message is the url to redirect:

HTML:

<form action="javascript:void(0);" id="advanced-search" method="post">
...
</form>

JS:

jQuery("#advanced-search").submit(function(e){
            //var formInput=jQuery(this).serializeObject();
            var formInput=jQuery(this).serializeJSON({parseBooleans: true,parseNulls: true});

            e.preventDefault();
            jQuery.ajax({
                url: "<s:url action='searchJSON'/>",
                data: "jsonForm="+JSON.stringify(formInput),
                contentType: "application/json",
                dataType: "json"
            }).done(function( msg ) {
                window.location.href="<s:url action='search'/>?"+msg;
            });
        });

Then in the method called by ajax, i transform the JSON object in an url:

In the Struts.xml:

<action name="searchJSON" class="searchAction" method="searchJSON">
    <result type="json">
        <param name="root">urlRedirectAjax</param>
    </result>
</action>

The method:

public class SearchAction extends ActionSupport {

private String  urlRedirectAjax;

public String searchJSON(){

    StringBuilder urlResultat = new StringBuilder();
    JSONObject jObj = (JSONObject) new JSONTokener(jsonForm).nextValue();

    if(jObj == null)
        return ERROR;


    if(jObj.has("elementJSON")){
        JSONObject jsObjMot = (JSONObject) jObj.get("elementJSON");
        String separator = "";
        for(Object key : jsObjMot.keySet()){
            urlResultat.append(separator);
            separator = ";";
            urlResultat.append((String) key);
        }   
        urlResultat.append("&");
    }   

    .....

    urlRedirectAjax = urlResultat.toString();

    return SUCCESS;
}

public String getUrlRedirectAjax() {
    return urlRedirectAjax;
}

public void setUrlRedirectAjax(String urlRedirectAjax) {
    this.urlRedirectAjax = urlRedirectAjax;
 }
}

The url received by the ajax method look like : param=value;value;value;&param=value;

It's possible to format it as needed. The url is completed in the javascript (host,etc.)

Each param in the URL has to be a property of the Action called by the url.

malak
  • 27
  • 7