Spring thymeleaf HTML tags and forms

Spring thymeleaf HTML tags and forms

Because Thymeleaf fully supports HTML5, there are essentially no limitations in creating complicated forms that the Spring Boot application can manage.

Form creation

Thymeleaf has several unique properties that are useful for constructing and manipulating forms:

  • th:field - - utilized to associate inputs with form-backing bean characteristics.
  • th:errors -Form validation errors are stored in the th: errors attribute.
  • th:errorclass - A CSS class that will be applied to a form input if it contains validation errors.
  • th:object -It is a backend attribute that carries a command object (main form bean object) that represents a form.

Spring Boot Controller

A controller class in a Spring Boot web application handles all HTTP requests. To indicate that a class is a web controller, it is annotated with the @Controller annotation.

Thymeleaf Templates

Thymeleaf templates are static HTML files that may be used in web apps and browsers. By default, these templates are kept in the src/main/resources/templates/ directory.

Command object

The command object is the form's principal form-backing bean, including all properties relevant to input fields. It's a Java model class with stated getter and setter methods, in general. Another th: object attribute cannot be defined within an HTML tag. Because HTML forms cannot be nested, this is the case.

Dropdown and Input fields

Using the th: field attribute, you can link an input field to a bean class property. Whether or not this attribute is linked to any tag affects how it appears.

The type of the input tag is likewise taken into account by the th: field. All contemporary HTML5 input types, such as type="email," type="number," type="color," type="date," and so on, are supported by Thymeleaf.