In MVC how is the data passed from JSP to Controller?

jsp, model-view-controller, spring

Solution

question: How the controller knows that from the form on the jsp Student class instance student with name, age and id are passed?

When you submit the form you are making an HTTP (typically, POST) request to a given URL. This POST request will contain the values in the form as request parameters. If you were not using any web framework (e.g. Spring MVC) then you would typically work directly with the Servlet API to extract and work with these values, particularly the `HttpServletRequest` object.

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

You can try this is your application by adding the following (the Spring MVC framework will automatically pass in the request).

public String addStudent(@ModelAttribute Student student, HttpServletRequest request){
    for(String key : request.getParameterMap().keySet()){
        System.out.println(key  + "=" + request.getParameterMap().get(key);
    }
}

Now, regardless of the framework you are using the underlying mechanism does not change, the parameters are still sent in the POST request as simple Strings. The framework however essentially adds an abstraction layer on top of this to prevent you having to write boilerplate to extract and manually work with these parameters. So, rather than having to do the following:

public String addStudent(HttpServletRequest request){
    Student student = new Student();
    student.setId(Integer.parseInt(request.getParameter("id"));
    student.setName(request.getParameter("name"));
    ....

}

you let the framework take care of it.

public String addStudent(@ModelAttribute Student student){

}

The `@ModelAttribute` tells the framework you want the submitted parameters to be bound to a Student instance. On submit, the framework will create a new Student instance and, by means of reflection, (http://docs.oracle.com/javase/tutorial/reflect/) set the various fields to the corresponding HTTP params.

As for the 2nd part of your question there are numerous examples of how to bind to a Collection. See below for example:

http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/

Problem

I'm following the Spring MVC tutorial here: http://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm and I'm not getting the logic how is the data passed from JSP to Controller. I think I understand how the data is passed from the Controller to the JSP, but after the user has edited the form on the JSP how is the data passed to the Controller? In the controller: public String addStudent(@ModelAttribute("SpringWeb")Student student, ModelMap model) - question: How the controller knows that from the form on the jsp Student class instance student with name, age and id are passed? I have this example working. I have altered the example to display a list of students, but I am not able to get the list from JSP to Controller: ``` @RequestMapping(value = "/student", method = RequestMethod.POST) public ModelAndView studentSave(@ModelAttribute("listOfStudents") ArrayList<Student> listOfStudents,ModelMap model) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); StudentJDBCTemplate studentJDBCTemplate = (StudentJDBCTemplate) context.getBean("studentJDBCTemplate"); System.out.println("Size of listOfStudents is = " + listOfStudents.size()); ... ``` `listOfStudents.size()` returns 0. - question: what am i missing here, why I can't get the list from the form on the jsp?

Original source