ThymeLeaf SpringInputGeneralFieldTagProcessor' 两个 th:fileds 冲突

ThymeLeaf SpringInputGeneralFieldTagProcessor' two th:fileds confilicting

当我使用创建操作时,更新操作会遇到问题 th:value=*{name} 如何设置 th:field 值不相互冲突。

错误如下:

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)

这里是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>

有了 simple spring-boot-starter(web,thymeleaf,lombok),这个简单的应用程序:

@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;
}

和简化的 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 没有问题:

所以(对我来说)事实证明,thymeleaf 使“无冲突”渲染 两个 <input/> 具有相同名称的不同甚至相同的字段(->列表属性)<form/>.

另一方面id不nice/html符合! (...不是针对 thymeleaf,而是针对客户端使用。)请解决它,例如喜欢:

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

使用 devtools,请确保:

spring.thymeleaf.cache=false

对于开发环境(仅!;),这将增加观察的可靠性。

...请按照堆栈跟踪进行到底! (我发现它们与其他模板框架完全相反:)。

我们可以通过(人为引入错字)快速重现一些类似的错误消息:

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

-> 重建,重新加载 ->

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