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](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue) viewName)
Convenient constructor when there is no model data to expose.
ModelAndView([String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue) viewName,[Map](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Futil%2FMap.html%3Fis-external%3Dtrue)<[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue),?> model)
Creates new ModelAndView given a view name and a model.
ModelAndView([String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue) viewName,[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue) modelName, [Object](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FObject.html%3Fis-external%3Dtrue) 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](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Futil%2FMap.html%3Fis-external%3Dtrue)<[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue),?> model)
Creates new ModelAndView given a View object and a model.
ModelAndView(View view,[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue) modelName,[Object](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FObject.html%3Fis-external%3Dtrue) modelObject)
Convenient constructor to take a single model object.
ModelAttribute的使用:
在方法定义上使用 @ModelAttribute 注解:
Spring MVC在调用目标处理方法前,会在每个目标方法前,先逐个调用在方法级上标注了@ModelAttribute 的方法。 在方法的入参前使用 @ModelAttribute 注解:
流程:1.执行@ModelAttribute注解的方法: 从数据库取出相关对象,自动把对象放到request的map中
2.SpringMvc 从map中取出相关对象,并把前台相关的表单属性赋值给这个对象