Spring:具有和不具有@ModelAttribute的绑定对象 [英] Spring : binding object with and without @ModelAttribute
问题描述
我是Spring的新用户并正在注册用户.我确实是这样.
I am new in Spring and registering a user.I did like this.
@RequestMapping("/register")
public String register(@ModelAttribute User user,BindingResult result){
if(!result.hasErrors()){
userSerive.register(user);
}
return "welcome";
}
这很好,但是这里的问题是我在welcome.jsp
页面中不需要此user
对象,所以为什么要使模型对象变重.所以我尝试了不使用@ModelAttribute
的情况,这对我也适用,如下所示
This worked fine,but problem here is I don't need this user
object in my welcome.jsp
page,so why make model object heavier.So I tried without @ModelAttribute
, this also works for me like below.
@RequestMapping("/register")
public String register(User user,BindingResult result){
if(!result.hasErrors()){
userSerive.register(user);
}
return "welcome";
}
所以我只想知道什么是优点和优点?和两者都有缺点,如果我真的不需要jsp
中的user
对象,这是最佳实践.是@ModelAttribute
除了将对象添加到模型之外还做其他事情,它隐含了弹簧绑定不是.@ModelAttribute
是更安全的绑定方式吗?
So I just want to know what are pros & cons of both and which is the best practice if I really don't need user
object in jsp
. Is @ModelAttribute
does any other thing apart from adding object to Model,which spring implicit binding not does.Is @ModelAttribute
safer way of binding or else?
我想将查询分为以下4种类型.@ModelAttribute
和没有@ModelAttribute
的区别是,如果我不需要在视图中发送数据并且我的请求是任何
I want to categories my query in following 4 type of request.what would be difference with and without @ModelAttribute
if I need not to send data in view and my request is any of-
- 查询字符串,即GET中的表单数据
- 请求有效载荷或主体,即POST中的表单数据 ajaxized GET requst中的
- json数据 POST请求中的
- json数据-我猜这两个都不会绑定.
@RequestBody
是必需的.
- query string ie form data in GET
- request payload or body ie form data in POST
- json data in ajaxified GET requst
- json data in POST requst- I guess this would not be bind in any of both.
@RequestBody
is required.
推荐答案
在这种情况下,两种方法签名之间的行为可能没有差异(见下文...).
There is probably (see below...) no difference in the behaviour between the two method signatures in your case.
两者都将请求参数绑定到user
,并将结果对象作为属性user
添加到模型中-该属性名称是从方法参数User
的大写字母类型名称派生的.
Both will bind the request parameters to user
and add the resulting object to the model as the attribute user
- this attribute name being derived from the decapitalised type name of the method argument, User
.
@ModelAttribute
可用于自定义属性名称,例如@ModelAttribute("theUser")
,或者向您的代码读者提示在视图中使用此参数.但是正如您所说,这些都不适用于您的用例.
@ModelAttribute
can be used to customise the name of the attribute, e.g. @ModelAttribute("theUser")
, or to give a hint to the reader of your code that this argument is used in the view. But as you say, neither of these apply in your use case.
无论您是否使用@ModelAttribute
批注,Spring中都将使用完全相同的代码填充参数-问题代码为org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor
.
Exactly the same code in Spring will be used to populate the argument whether you use the @ModelAttribute
annotation or not - the code in question is org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor
.
因此,对于我来说,在代码中使用public String register(User user, BindingResult result)
签名对我来说更有意义.在模型中不需要的方法参数上添加@ModelAttribute
批注可能会使阅读代码的人感到困惑.
It therefore makes more sense to me for you to use the public String register(User user, BindingResult result)
signature in your code. Adding a @ModelAttribute
annotation to method arguments that are not required in the model could be confusing to people reading your code.
稍长一点的答案是,在您的情况下指定@ModelAttribute
可能是大约的原因-但这相当不可思议,而且不太可能.
The slightly longer answer is that there could just about be a reason for specifying @ModelAttribute
in your case - but it's quite arcane and unlikely.
方法参数由HandlerMethodArgumentResolver
实例填充.这些是可配置的,并针对每个参数依次尝试.
Method arguments in Spring handler methods are populated by HandlerMethodArgumentResolver
instances. These are configurable and are attempted in turn for each parameter.
默认的处理程序方法参数解析器如下所示(请参见RequestMappingHandlerAdapter
):
The default handler method argument resolvers look like this (see RequestMappingHandlerAdapter
):
resolvers.add(new ServletModelAttributeMethodProcessor(false));
...
resolvers.add(new ServletModelAttributeMethodProcessor(true));
如果要在中间添加自己的内容,例如UserHandlerMethodArgumentResolver
,则可以使用@ModelAttribute
告诉Spring以默认方式处理特定参数,而不是使用自定义参数解析程序类.
If you were to add your own in the middle, e.g. a UserHandlerMethodArgumentResolver
, you could then use @ModelAttribute
to tell Spring to process a specific argument in the default way, rather than use your custom argument resolver class.
这篇关于Spring:具有和不具有@ModelAttribute的绑定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!