Four methods of transferring data to page in Spring MVC

When we used Servlet + JSP to develop web applications, we usually used the four scopes of Servlet: request,session,page,application to pass values to the page. Spring MVC certainly supports the native APIs of these servlets, but it also provides us with more powerful API support. Let's discuss how spring MVC transfers data to pages.

I. Map

The first is Map. Yes, it is the Map interface in the JDK. We just need to put the data to be transferred into the Map in the way of key value pairs like the HashMap key value pairs. Spring MVC will put it into the request domain and pass it to the page.

@Controller
public class OutputController {

    @GetMapping("/handle1")
    public String handle(Map<String, Object> map) {
        map.put("msg", "Hello Map!");
        return "success";
    }
    
}

Enter localhost:8080/handle1 in the browser

Two, Model

Model is the meaning of model. As the name implies, it is used to transfer data. We only need to call the addAttribute() method of model object to add the information to be transferred into the model in the way of key value pairs, and then we can transfer the data to the page.

@Controller
public class OutputController {

    @GetMapping("/handle2")
    public String handle(Model model) {
        model.addAttribute("msg", "Hello Model!");
        return "success";
    }
    
}

Enter localhost:8080/handle2 in the browser

3, ModelMap

ModelMap is the implementation class of Map, but it is called ModelMap, which means that it integrates the characteristics of Model, so it is similar to Model in use.

@Controller
public class OutputController {

    @GetMapping("/handle3")
    public String handle(ModelMap mp) {
        mp.addAttribute("msg", "Hello ModelMap!");
        return "success";
    }
    
}

Enter localhost:8080/handle3 in the browser

4, ModelAndView

In the past, ModelAndView should be the most common return value type when the front and back ends are not separated. Now after the front and back ends are separated, the back end mainly returns JSON data. It is easy to understand that the backend returns ModelAndView. Developers can specify the view name in the ModelAndView object, or bind data.

@Controller
public class OutputController {

    @GetMapping("/handle4")
    public ModelAndView handle() {
        ModelAndView mv = new ModelAndView("success");
        mv.addObject("msg", "Hello ModelAndView!");
        return mv;
    }
    
}

Enter localhost:8080/handle4 in the browser

Extension: relationship among Map, Model and ModelMap

In each handle() method, we add System.out.println("xxx type:" + xxx.getClass()); and print their types in the console.

The results are always strikingly similar

Their types are

org.springframework.validation.support.BindingAwareModelMap

We found this class

public class BindingAwareModelMap extends ExtendedModelMap {

	@Override
	public Object put(String key, Object value) {
		removeBindingResultIfNecessary(key, value);
		return super.put(key, value);
	}
    
}

Found that it inherits from ExtendedModelMap

public class ExtendedModelMap extends ModelMap implements Model {

	@Override
	public ExtendedModelMap addAttribute(String attributeName, @Nullable Object attributeValue) {
		super.addAttribute(attributeName, attributeValue);
		return this;
	}
    
}

The answer is very clear. This class implements not only inherits the ModelMap class, but also implements the Model interface. There is still ModelMap left

public class ModelMap extends LinkedHashMap<String, Object> {

	/**
	 * Construct a new, empty {@code ModelMap}.
	 */
	public ModelMap() {
	}
	
}

ModelMap inherits from LinkedHashMap, which is the implementation class of Map, needless to say.

Here is a diagram to clearly show the relationship between them.

50 original articles published, 96 praised, 60000 visitors+
Private letter follow

Keywords: Spring JSP Session JDK

Added by cyberdwarf on Sat, 22 Feb 2020 11:46:10 +0200