0

I need to send an array from my JSP page to a Spring Controller. In my JSP I get my array full with the things I need, and then, I start an AJAX request:

var ajaxRequest;  

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

    ajaxRequest.open('POST', 'saveRecompensas.action');

    ajaxRequest.send('array='+array);

In the Controller:

@RequestMapping ("/proyecto/saveRecompensas")
public String saveRecompensas(@RequestParam ("array") String[] array, HttpSession session){
//public String saveRecompensas(){  
System.out.println(Method saveRecompensas called");

    return null;        
}

As you can see, I have two methods signature. That is because, if I use the one without the @RequestParam ("array") String[] array, the method is called ok. But I need that array. If I use with the RequestParam, the method is never called.

Is this the correct way to send and array from JSP to Controller? What Im doing wrong? Thank you.

Fustigador
  • 6,339
  • 12
  • 59
  • 115
  • I think that your problem is because array works different you have to send multiple element with the same name. var array = ''; for (var i=0; i – Makoton Jul 30 '14 at 14:30
  • Even in a POST petition? Its not GET, the params are not stored in the URL. – Fustigador Jul 30 '14 at 14:48
  • I'm sorry now i see, but with post you can send parameter by url and the controller catch them(you can test and work). but i think the best solution is serialize the data. http://stackoverflow.com/questions/15173965/serializing-and-submitting-a-form-with-jquery-post-and-php . Other solution that you can try is assigned the position for every array like: 'array[0] =val2" this may be work too – Makoton Jul 30 '14 at 15:24

1 Answers1

1

Your problem is data binding related in that you aren't following the conventions of how data binding works in Spring. So Spring has a specific way it will do data binding. Data binding is the process of taking a stream of data and turning it into a Objects and native data types in a language, or binding the data from the stream into a series of Objects. You can have Spring do this for you or you can do it yourself. Given that you are writing everything the hard way I'm going to assume you like the do it yourself model. So let's skip the automatic type conversion explanation and here's the "I can do it myself daddy" way.

  1. Change your @RequestParam("array") String[] array -> @RequestParam("array") String inputArray
  2. In the body of your method String[] array = inputArray.split(",")

Viola you have your array. Although your method of adding the parameters in Javascript has problems because you aren't using encodeURI() to properly escape strings this method will have problems when your input changes.

Now if you're curious about more complex data binding or making your example work. You need to encode this the way Spring understands. That has to do with how you are doing things in Javascript. Spring will realize something is an array if the parameter name occurs multiple times on the parameter string. For example,

http://somedomain.com/some/url?array=first&array=second&array=third

If you encode your array like this:

ajaxRequest.send( array.reduce( function( prev, curr ) { 
   return prev + (prev.length > 0 ? "&" : "") + "array=" + encodeURI( curr ); 
}, "" ) );

Notice that the encodeURI( curr ) call surrounds the value of the array so input with things like spaces won't screw up the call to the server.

Finally if you really do this thing a lot you may want to consider using JSON to send data because that allows for much more complex data structures on the server (Objects, etc).

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
  • Thanks for your answer. I am not an expert Spring coder, as you can imagine...I'been trying to pass the data to the controller the way I have seen in other questions here. I have done what you said first (The "I can do myself daddy" way, as you said in your answer) and the method is not called. Encoding the array the way you said is not working neither...the method is never called. So, if you are so kind, I would like to know the "I can not do it by myself, daddy". If I am offending you with my question, you don't have to answer if you don't want to. – Fustigador Jul 30 '14 at 13:49
  • Ah. Your javascript says the URL is 'saveRecompensas.action' but your RequestMapping says "/proyecto/saveRecompensas". Try changing the URL in javascript to match the URL in Spring (remember to add the context path to that URL if you want an absolute URL). – chubbsondubs Jul 30 '14 at 14:05
  • But if I remove that signature (leaving simply public String saveRecompensas()) the method is called ok, and the sysout in the method shows out, so I "think" it could be another sort of problem...If the problem would be that, the method would never be called, if Im not in a mistake. – Fustigador Jul 30 '14 at 14:08
  • How is the "I can not do it myself, daddy" way to do it? – Fustigador Jul 30 '14 at 15:14
  • I think you may have misunderstood my advice. Change the javascript from ajaxRequest.open('POST', 'saveRecompensas.action'); to ajaxRequest.open('POST', 'saveRecompensas'); The reason your method isn't being invoked is because the @RequestMapping defines a URL that doesn't end in .action So javascript should not use saveRecompensas.action. – chubbsondubs Jul 31 '14 at 01:55