3

I am new to spring mvc and have seen examples where folks use @ModelAttribute("form") Person person in method parameter and some who just do (Person person). what is the difference.

Thanks in advance

2 Answers 2

3

If you don't specify @ModelAttribute in this situation, then Spring will infer the presence of one. In this case, it would infer @ModelAttribute("person"). The argument is the name of the model.

If you don't want the default name, or if you just want to be explicit about saying that the Person parameter is a model attribute, then you should specify @ModelAttribute, with or without the explicit name.

Sign up to request clarification or add additional context in comments.

Comments

2

@ModelAttribute allows you to specify the name of the model attribute. When parameter doesn't have this annotation, a decapitalized class name is used as an attribute name, i.e. Person p -> "person".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.