Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to have the value of a hidden input field as an array and then have it passed to the Spring MVC controller?

function foo(){
 var myArray = new Array();
 myArray[0] = "Hello";
 myArray[1] = "World!";
 document.getElementById("hiddenInp").value=myArray;
}

And then in the controller do something like

@RequestMapping ...
public String test(HttpServletRequest request){
String[] myArray = request.getParameter("hiddenInp");
// Assuming that the name of the hidden input field is also hiddenInp
System.out.println(myArray[0] + myArray[1]);
...
}

How about if I am working with an associative array? Where the indices are string rather than int

share|improve this question
JavaScript doesn't have associative arrays. – Jonathan Sampson Jun 7 '12 at 21:25
@JonathanSampson Maybe I'm using the wrong term. I meant, instead of index such as "0" and "1", I do myArray["A"] = "Hello";. I know that works because I've tried alert(myArray["A"]) and it gives me the pop-up box with "Hello". – szrrizvi Jun 7 '12 at 21:29
@JonathanSampson: But objects can be (and are) used as maps... – Bergi Jun 7 '12 at 21:30
@Bergi Yes; objects can have keys, but they're not associative arrays. One big difference is the order of values. With an array the order is fixed; not so with objects and their properties. – Jonathan Sampson Jun 7 '12 at 21:31
@szrrizvi You're referring to an object: var myArray = { 'A' : 'Hello' };. – Jonathan Sampson Jun 7 '12 at 21:32

3 Answers

up vote 2 down vote accepted

The elements value cannot be an array (values are strings). The array will be cast to a string, and joined by a comma, but you can provide your own seperator by calling join manually:

document.getElementById("hiddenInp").value = myArray.join(" ");

Which results in:

"Hello World!"

Fiddle: http://jsfiddle.net/jonathansampson/HTqww/

share|improve this answer
not necessary to do. setting the value as an array will do exactly what .join() does. using .join() is just extra processing for nothing. – Ian Jun 7 '12 at 21:11
@ianpgall Noted. I tend to forget that JavaScript will automatically handle the casting of an array to the appropriate type and value. – Jonathan Sampson Jun 7 '12 at 21:15
i hadn't expected it to work - i thought it might include "[" and "]" but it ended up working like .join() anyways. no problem, just wanted to point it out! – Ian Jun 7 '12 at 21:16
How about if I have an associative array? Where the indicies are string rather then numbers. – szrrizvi Jun 7 '12 at 21:20
1  
@ianpgall - Is .join() really extra processing though? If the value you set is already a string JS doesn't have to convert it, so it's not like it processes it twice. Plus of course .join() gives you explicit control over the joining character(s). – nnnnnn Jun 7 '12 at 21:20
show 1 more comment

However you set it in Javascript, you'll have to parse it on the server. That probably means splitting the value by "," into a C# array, then accessing it as you want. All values sent in a form submission are sent as a string exactly as-is.

You might want to use http://www.w3schools.com/jsref/jsref_join.asp to set the value of the hidden input. It's not necessary though - it seems to be fine without using .join(). But it's important to remember that the value will be a string on the server, not an array.

share|improve this answer
have a look at w3fools.com – Bergi Jun 7 '12 at 21:28
Yes, I saw that a long time ago. While I think that's funny, it has nothing to do with using w3 schools for simple references. I provided the link so the OP can use the syntax correctly, not as a solution (especially since I'm saying .join() shouldn't be used). – Ian Jun 7 '12 at 21:34

You can only pass a string as a parameter, as well as to an input's value. That means you Array will automatically be converted to a string by joining it with ";", you could do that manually with the .join() method, too.

Serverside you then will need to split that string by the chosen delimiter.

If you want to send them as an array, afaik you will need two input elements:

<input type="hidden" name="hiddenInp[]"<!-- should be more descriptive --> value="a" />
<input type="hidden" name="hiddenInp[]"<!-- the same name --> value="b" />
share|improve this answer
you can set an input's value as an array. javascript basically does what .join() WOULD do, but it's not necessary to use .join() unless you want a delimiter other than "," – Ian Jun 7 '12 at 21:15
That's exactly what I said. However, I would even use .join(",") to make it clear that I'm setting a string. – Bergi Jun 7 '12 at 21:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.