In Spring 3, I have seen two different attribute in form tag in jsp
<form:form method="post" modelAttribute="login">
in this the attribute modelAttribute is the name of the form object whose properties are used to populate the form. And I used it in posting a form and in controller I have used @ModelAttribute
to capture value, calling validator, applying business logic. Everything is fine here. Now
<form:form method="post" commandName="login">
What is expected by this attribute, is it also a form object whose properties we are going to populate?
In xml based config, we will use command class to pass an object between controller and views. Now in annotation we are using
modelattribute
.I had the same question a while ago, I can't remember the exact differences but from research I ascertained that
commandName
was the old way of doing it and in new applications you should be usingmodelAttribute
OLD WAY = commandName
NEW WAY = modelAttribute
If you look at the source code of
FormTag
(4.3.x) which backs your<form>
element, you'll notice thisThey are both referring to the same field, thus having same effect.
But, as the field name indicates,
modelAttribute
should be preferred, as others have also pointed out.commandName = name of a variable in the request scope or session scope that contains the information about this form,or this is model for this view. Tt should be a been.