Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between modelAttribute and commandName attributes in form tag in spring?

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?

like image 921
Pulkit Avatar asked Feb 01 '14 07:02

Pulkit


People also ask

What is commandName form tag?

The commandName attribute is the most important attribute in the form tag, which specifies the model attribute name that contains a backing object and the properties of this object will be used to populate the generated form.

What is the difference between model and @ModelAttribute?

As Model needs a pair of name and value to populate, @ModelAttribute element 'value' is used as attribute name and the method returned object is used as value. If no 'value' is specified in @ModelAttribute then the returned type is used as the attribute name.

What is model attribute in form tag?

@ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute, and then exposes it to a web view. In this tutorial, we'll demonstrate the usability and functionality of this annotation through a common concept, a form submitted from a company's employee.


2 Answers

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.

like image 73
Sotirios Delimanolis Avatar answered Oct 04 '22 01:10

Sotirios Delimanolis


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> .. 
like image 32
diego matos - keke Avatar answered Oct 04 '22 01:10

diego matos - keke