Modification in conventional ModelAndView

In this section, you will get to known about the new modification in conventional ModelAndView.

Modification in conventional ModelAndView

Modification in conventional ModelAndView

In this section, you will get to known about the new modification in conventional ModelAndView.

The modifications in ModelAndView is inspired by the ModelMap class and ModelAndView also uses ModelMap to implement those inspired modifications.

In ModelMap , if a single object is added using addObject() method, a key is automatically generated by it. Also, when an object is added to the ModelAndView ,  it uses ModelMap to automatically generate associated key.

To understand it completely, take a look at the below controller code :

public class WritingController implements Controller {

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {

	List selectedItems = // get a List of selected objects
	Employee employee = //get the instance of the class Employee 
	
	ModelAndView model= new ModelAndView("displayUserAccount"); <-- the logical view name
	
	model.addObject(selectedItems); <-- no name, just the object
	model.addObject(employee); <-- again an object is added with no name
	
	return model;
}
}

Two things comes out from above example :

First, you can add multiple object to the same ModelAndView.

Second thing is that a list and class instance is added but no name is added with objects. Now, the name is resolved under the following rules :

Rules for automatic resolution of the name of the added scalar object such as a class instance is :

  • If a a.b.Employee class instance is added, name employee will be generated.

  • If a.b.Payroll instance is added , the name payroll will be generated.

  • If java.util.HashMap instance is added, the name hashMap will be generated.

  • If null will be added, an IllegalArgumentException will be thrown.

Rules for automatic resolution of the name of the added collections object such as a Set or a List is :

  • If no or , zero or more x.y.Employee element is added to x.y.Employee[] array, name employeeList will be generated.

  • If no or , zero or more x.y.Employee element is added to x.y.Foo[] array, name fooList will be generated.

  • If one or more a.b.Employee elements are added to java.util.ArrayList, the name employeeList will be generated.

  • If one or more a.b.Foo elements are added to java.util.HashSet, the name fooList will be generated.

  • An empty java.util.ArrayList should not be added.