What is the advantage of form:label tag in Spring?

12,029

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.

Share:
12,029
Kshitiz Sharma
Author by

Kshitiz Sharma

Developer who enjoys sharing knowledge. https://ksharma.dev Open source projects: Github

Updated on June 07, 2022

Comments

  • Kshitiz Sharma
    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
    Kshitiz Sharma about 10 years
    For i18n I could use - <label for="company"><spring:message code="label.title.companyNameInput"/></label>. Where label.title.companyNameInput is the key into the resource bundle. No need for form:label.
  • Menuka Ishan
    Menuka Ishan almost 8 years
    Localization not need any spring tags. Don't post things you assume.