SpringMVC 之 ModelAndView和ModelAttribute的使用

ModelAndView解释 :

This class merely holds both to make it possible for a controller to return both model and view in a single return value.

这类控制器可以同时返回视图和模型数据.


ModelAndView使用:

ModelAndView()

Default constructor for bean-style usage: populating bean properties instead of passing in constructor arguments.

ModelAndView(String viewName)

Convenient constructor when there is no model data to expose.

ModelAndView(String viewName,Map<String,?> model)

Creates new ModelAndView given a view name and a model.

ModelAndView(String viewName,String modelName, Object modelObject)

Convenient constructor to take a single model object.

ModelAndView(View view)

Convenient constructor when there is no model data to expose.

ModelAndView(View view,Map<String,?> model)

Creates new ModelAndView given a View object and a model.

ModelAndView(View view,String modelName,Object modelObject)

Convenient constructor to take a single model object.

ModelAttribute的使用:

SpringMVC 之 ModelAndView和ModelAttribute的使用


在方法定义上使用 @ModelAttribute 注解:
    Spring MVC在调用目标处理方法前,会在每个目标方法前,先逐个调用在方法级上标注了@ModelAttribute 的方法。 在方法的入参前使用 @ModelAttribute 注解:

    流程:1.执行@ModelAttribute注解的方法: 从数据库取出相关对象,自动把对象放到request的map中

            2.SpringMvc 从map中取出相关对象,并把前台相关的表单属性赋值给这个对象

转载于:https://my.oschina.net/giegie/blog/425908