Spring MVC and Thymeleaf - What's the difference between th:value and th:field when you iterate multiple items?

Jin Lim :

I want to display multiple items from database to Thymeleaf view, using HTML Form so that I can make a change and update to the database.

I was going to use th:field . but it occurred error. when I tried to use th:value. and It shows data.

<div th:each="item : ${courses}">
  <form th:object="${item}">
    <input th:field="*{name}" type="text" id="name" name="name" /> // error occur
  </form>
</div>

error following.

Neither BindingResult nor plain target object for bean name 'item' available as request attribute

Whereas this below code works fine.

<div th:each="item : ${courses}">
  <form th:object="${item}">
    <input th:value="*{name}" type="text" id="name" name="name" /> // ok
  </form>
</div>

Am I on the right track? Can you explain why? and the difference between field and value?

Metroids :

The bottom line is that you can only use th:object and th:field together on a base model attribute. That means you can't use it when iterating (because ${item} doesn't exist on the model, it's a variable generated by the th:each). The requirements are spelled out here:

Values for th:object attributes in form tags must be variable expressions (${...}) specifying only the name of a model attribute, without property navigation. This means that an expression like ${seedStarter} is valid, but ${seedStarter.data} would not be.

Once inside the <form> tag, no other th:object attribute can be specified. This is consistent with the fact that HTML forms cannot be nested.

th:field sets the name, id, and value of a field. So they are somewhat interchangeable but you should use th:field whenever possible because it offers the extra integration with spring, and works on all kinds of input -- but these are really only useful when you are editing a single object on a page.

Since you have multiple objects you are iterating, you're going to have to manually set the name, id, and value like you are doing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What's the difference between data-th-text and th:text in Spring thymeleaf

Thymeleaf th:field overwrites th:value

Spring MVC - Thymeleaf th:action th:object

Difference between th:href and href when linking absolute URLs in Thymeleaf

thymeleaf th:field returns erros when i use the th:field

Difference between th:with and th:if

th:field parameter syntax using Thymeleaf in Spring

Thymeleaf th:value

Spring AnnotationFormatterFactory with Thymeleaf th:text, only formats when field is explicitly string

Value attribute is missing after adding thymeleaf's tag th:field to input

th:href Thymeleaf redirect url with path variables and object id's with Spring MVC

Thymeleaf + jQuery , how to properly add th:field="*{value}"

Thymeleaf: cannot pass a predefined value of hidden th:field

Does Thymeleaf's th:field generate an id attribute too?

Difference between spring JSP MVC and Thymeleaf MVC

Thymeleaf: th:value - if property exists

Thymeleaf th:each two iterate two listings

Unable to substitute value of variable in Thymeleaf's th:src tag

What's the difference between Type.Field and Value.Field?

Thymeleaf th:field pre-processing is not working with th:each

Thymeleaf - Only print row if (th:field is != null)

what's difference between Controller and Handler in Spring MVC?

What does the Thymeleaf tag "th:for" do?

Spring Thymeleaf collapse table with th:id

Set th:value in Thymeleaf date input?

thymeleaf th:if - Cannot index into a null value

I study thymeleaf but i don't know what does it mean this th:field from this <select>

Thymeleaf th:each filtered with th:if

thymeleaf block is not show when condition of th:if is true