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的使用:

110835_SsTF_2303656.png


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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值