Java – what does the @ modelattribute annotation at the method parameter level mean?

The spring 3 reference tells us:

I don't understand this magic spell because I'm sure that the alias of the model object (if modelmap is used as the key value of the return type) is passed to view after executing the request handler method Therefore, when the request handler method executes, the name of the model object cannot be mapped to the method parameter

To solve this contradiction, I went to stack overflow and found a detailed example of this The author of the example said:

It seems that he is attracted by spring references

To eliminate the charm, I deployed his sample application in my environment and started from the method maincontroller The @ modelattribute annotation is left in saveedit() As a result, the application can run without any changes! So my conclusion is that passing the field value of the web form to the field of the parameter does not need the @ modelattribute annotation Then I insist on this question: what does the @ modelattribute annotation mean? If the only meaning is to set aliases for model objects in view, why is this better than explicitly adding objects to modelmap?

Solution

The key is that @ modelattribute is optional - if the parameter has no comments and its type has no special meaning (i.e. it is not HttpServletRequest, modelmap, etc.), it is regarded as @ modelattribute annotated parameter

Therefore, you need @ modelattribute effectively in two cases:

>Specifies the name of the property If @ modelattribute is omitted or has a null value, the default name (the type name of the parameter with the first letter decapitalized) is used. > If the type of the parameter has a special meaning For example, if your domain object is passed as an attribute, extend Java security. Principal, you need to annotate it, otherwise spring will pass HttpServletRequest The result of getuserprincipal()

Some people prefer to use @ modelattribute to record the meaning of parameters without actual needs

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>