Spring:@ModelAttribute VS @RequestBody [英] Spring: @ModelAttribute VS @RequestBody

查看:635
本文介绍了Spring:@ModelAttribute VS @RequestBody的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我错了,请更正我。
两者都可以用于数据绑定

Please correct me if I am wrong. Both can be used for Data Binding.

问题是何时使用@ModelAttribute?

The question is when to use @ModelAttribute?

@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) { }

另外,何时使用@RequestBody? p>

In addition, when to use @RequestBody?

@RequestMapping(value = "/user/savecontact", method = RequestMethod.POST
public String saveContact(@RequestBody Contact contact){ }

根据我的理解,两者都有类似的用途。

According to my understanding both serves the similar purpose.

谢谢!!

推荐答案

我理解的最简单的方法是, @ModelAttribute 将采用一个查询字符串,所以所有的数据都通过url传递给服务器。

The simplest way for my understanding is, the @ModelAttribute will take a query string. so, all the data are being pass to the server through the url.

至于 @RequestBody ,所有数据将通过完整的JSON正文传递给服务器。

As for @RequestBody, all the data will be pass to the server through a full JSON body.

这篇关于Spring:@ModelAttribute VS @RequestBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆