Difference between modelAttribute and commandName attributes in form tag in spring?
Asked Answered
F

5

101

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?

Farrahfarrand answered 1/2, 2014 at 7:57 Comment(0)
I
133

If you look at the source code of FormTag (4.3.x) which backs your <form> element, you'll notice this

/**
 * Set the name of the form attribute in the model.
 * <p>May be a runtime expression.
 */
public void setModelAttribute(String modelAttribute) {
    this.modelAttribute = modelAttribute;
}

/**
 * Get the name of the form attribute in the model.
 */
protected String getModelAttribute() {
    return this.modelAttribute;
}

/**
 * Set the name of the form attribute in the model.
 * <p>May be a runtime expression.
 * @see #setModelAttribute
 */
public void setCommandName(String commandName) {
    this.modelAttribute = commandName;
}

/**
 * Get the name of the form attribute in the model.
 * @see #getModelAttribute
 */
protected String getCommandName() {
    return this.modelAttribute;
}

They 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.

Infralapsarian answered 1/2, 2014 at 15:48 Comment(2)
Good! How did you find out the name the class related to the from tag?Schroth
@Sangdol Conventionally, the class is just called <tag-name>Tag. For the fully qualified class name, open the library (.jar) containing the tag, spring-web in this case. Under META-INF, you'll find spring-form.tld. It'll have a <tag> entry for form with a <tag-class> of org.springframework.web.servlet.tags.form.FormTag.Infralapsarian
G
25

OLD WAY = commandName

...
<spring:url value="/manage/add.do" var="action" />
    <form:form action="${action}" commandName="employee">
        <div>
            <table>
....

NEW WAY = modelAttribute

..
<spring:url value="/manage/add.do" var="action" />
    <form:form action="${action}" modelAttribute="employee">
        <div>
            <table>
..
Gamo answered 19/1, 2015 at 16:38 Comment(0)
R
14

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 using modelAttribute

Rosenzweig answered 1/2, 2014 at 8:52 Comment(0)
O
1

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.

Outpouring answered 1/2, 2016 at 6:11 Comment(0)
V
-3

In xml based config, we will use command class to pass an object between controller and views. Now in annotation we are using modelattribute.

Versify answered 23/11, 2015 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.