What is the advantage of form:label tag in Spring?
Solution 1
The <form:label />
tag has access to the underlying model and binding results and as such can, on error, use another style class.
<form:label cssClass="title" cssErrorClass="title error" path="company" />
The code above would, in case of an error, render differently than the normal case. Of course you could also do this without the form tag but that would mean you need to include some logic into your pages, which in general isn't advised.
For all the properties see the reference guide
Solution 2
I really like using form:xxxx instead of the more traditional tags.
However, it seems to work best with PUT requests where I am modifying something, and maybe DELETE. With POST where everything is new, I tend to end up using the traditional tags.
It does link the human readable to the value being sent for automated readers, but both can do that.
Kshitiz Sharma
Developer who enjoys sharing knowledge. https://ksharma.dev Open source projects: Github
Updated on June 07, 2022Comments
-
Kshitiz Sharma almost 2 years
<form:label path="company">Enter company name:</form:label>
Renders -
<label for="company">Enter company name:</label>
Why shouldn't I directly use the HTML tag which is more concise?
-
Kshitiz Sharma about 10 yearsFor i18n I could use -
<label for="company"><spring:message code="label.title.companyNameInput"/></label>
. Wherelabel.title.companyNameInput
is the key into the resource bundle. No need forform:label
. -
Menuka Ishan almost 8 yearsLocalization not need any spring tags. Don't post things you assume.