ThymeLeaf SpringInputGeneralFieldTagProcessor' two th:fileds confilicting

Viewed 228

When I use create action is make me a problem for update action th:value=*{name} How to set th:field values to not conflict each other.

Here is the error:

There was an unexpected error (type=Internal Server Error, status=500). Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "designations" - line 243, col 18)

Here is html

   <div class="modal-body">
    <form th:action="@{/designations/create}" th:object="${newDesignation}" th:method="post">
        <div class="form-group">
            <label>Designation Name <span class="text-danger">*</span></label>
            <input th:field="*{name}" class="form-control" type="text">
        </div>
        <div class="form-group">
            <label>Department <span class="text-danger">*</span></label>
            <select th:field="*{departmentName}" class="select">
                <option th:each="i : ${departmentsList}" th:value="${i.name}" ></option>
            </select>
        </div>
        <div class="submit-section">
            <button class="btn btn-primary submit-btn">Submit</button>
        </div>
    </form>
</div>

    <div class="modal-body">
    <form th:action="@{/designations/update/{id}(id=${i.id})}" th:object="${designationToUpdate}" th:method="post">
        <div class="form-group">
            <label>Designation Name <span class="text-danger">*</span></label>
            <input th:field="*{name}" class="form-control"  type="text">
        </div>
        <div class="form-group">
            <label>Department <span class="text-danger">*</span></label>
            <select th:field="*{departmentName}" class="select">
                <option th:each="i : ${departmentsList}" th:value="${i.name}" th:text="${i.name}"></option>
            </select>
        </div>
        <div class="submit-section">
            <button class="btn btn-primary submit-btn">Save</button>
        </div>
    </form>
</div>
1 Answers

With simple spring-boot-starter(web,thymeleaf,lombok), this simple app:

@Controller
@SpringBootApplication
public class ThymeleafTestApplication {

  public static void main(String[] args) {
    SpringApplication.run(ThymeleafTestApplication.class, args);
  }

  @GetMapping
  public String test() {
    return "test";
  }

  @ModelAttribute("newDesignation")
  public MyDto newDesignation() {
    return new MyDto("foo");
  }

  @ModelAttribute("designationToUpdate")
  public MyDto designationToUpdate() {
    return new MyDto("bar");
  }

}

@Data
@AllArgsConstructor
class MyDto {
  String name;
}

and a simplified html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" lang="en">
  <head>
    <title>Test same input name</title>
  </head>
  <body>
    <h2>Test same input name</h2>
    <form th:action="@{/foo}" th:object="${newDesignation}" th:method="post">
      <div>
        <label>Designation Name <span>*</span></label>
        <!-- recommended to use an alternative/identifiyng th:id here:-->
        <input th:field="*{name}" type="text" />
      </div>
      <div>
        <button>Submit</button>
      </div>
    </form>
    <form th:action="@{/bar}" th:object="${designationToUpdate}" th:method="post">
      <div>
        <label>Designation Name <span>*</span></label>
        <!-- ... and here: -->
        <input th:field="*{name}" type="text" />
      </div>
      <div>
        <button>Save</button>
      </div>
    </form>
  </body>
</html>

Thymeleaf makes no problems:

Thymeleaf makes no problems screenshot

So (for me) it is proven, that thymeleaf makes "no conflicts" rendering two <input/> fields with identical names in different or even same (->list attribute) <form/>.

The duplicate id on the other hand is not nice/html conform! (...not for thymeleaf, but for client side usage.) Please resolve it, e.g. like:

  <input th:id="newName" ...
  <!-- ... and -->
  <input th:id="updateName" ...

With devtools, please ensure:

spring.thymeleaf.cache=false

for development environment (ONLY!;), this will increase the reliability of observations.

...and please follow the stack trace to the end! (I find them quite speaking in opposite to other templating frameworks:).

We can quickly reproduce some similar error message by (artificially introducing a typo):

@Data
@AllArgsConstructor
class MyDto {
  String namy; //!
}

-> rebuild, reload ->

2021-12-30 02:32:53.104 ERROR 20564 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine:
  [THYMELEAF][http-nio-8080-exec-1] Exception processing template "test":
    An error happened during template parsing (template: "class path resource [templates/test.html]")
#... like 1000 lines of stacktrace, referring to (first occurence of) *{name}, but then:
Caused by: 
  org.springframework.beans.NotReadablePropertyException: 
    Invalid property 'name' of bean class [com.example.thymeleaf.test.MyDto]:
      Bean property 'name' is not readable or has an invalid getter method:
        Does the return type of the getter match the parameter type of the setter?
#... few (hundred) more
Related