How do to pass a ArrayList
of Array
from Jsp to spring controller.
I want my user fill some form in which 1 object data is entered in many way. What I want to make a list and save all the different values of that object.
Then i want to pass it to spring controller. What is t开发者_Python百科he best way for this? Thanks.
Yes, Create Class and bind the this class to form so the controller will automatically receive the class.
It will look something like this.
@RequestMapping(value="/submit",method = RequestMethod.POST)
public ModelAndView addUser(@Valid MyClass myClass, BindingResult result){
}
public class MyClass{
private List<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
//getters and setters.
}
hope it helps.
Update:
Ajax way:
JSON = {"myClass" "list":[[1,2,3,4],[1,2,3,45],[3,5,1]]}
and the Controller itself.
@RequestMapping(value="/submit",method = RequestMethod.POST)
@ResponseBody
public String getAjax(@RequestBody Myclass myClass){
//do something
}
and the use javascript to build suck JSON and send it to server using AJAX. You can validate your form before that using Javascript as well.
First of all, you'll be passing an HTTP request from a JSP to a Spring controller, not an array of arrays.
The form that your users fill out will have HTML form elements that will be sent to the server side as HTTP parameter name/value pairs.
The Spring controller will validate and bind the form elements into objects; you can certainly bind all those parameters into an array of arrays if you wish.
精彩评论