【问题标题】:ThymeLeaf SpringInputGeneralFieldTagProcessor' two th:fileds confilictingThymeLeaf SpringInputGeneralFieldTagProcessor'两个th:文件冲突
【发布时间】:2021-12-30 10:18:58
【问题描述】:

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

这是错误:

出现意外错误(类型=内部服务器错误,状态=500)。 执行处理器“org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor”时出错(模板:“designations” - 第 243 行,第 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>

【问题讨论】:

  • 抱歉,不能(仅部分)复制:确保:第 243 行,第 18 列指向 *{name}(第一还是第二??),spring.thymeleaf.cache=false 并注意:devtools+lombok+(ide行为)(这就是我可以部分重现问题的方式(特别是在这个地方):通过弄乱名称的 getter/setter,忽略模板缓存,热重载)

标签: java html spring-boot thymeleaf


【解决方案1】:

有了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 使“没有冲突”渲染两个 &lt;input/&gt; 字段在不同甚至相同(->列表属性)&lt;form/&gt; 中具有相同的名称。

另一方面,重复的id 不是很好/html 符合! (......不是百里香,而是客户端使用。)请解决它,例如喜欢:

  <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

【讨论】:

  • 谢谢你的帮助
猜你喜欢
  • 2018-06-29
  • 1970-01-01
  • 2010-12-13
  • 2019-10-17
  • 1970-01-01
  • 1970-01-01
  • 2018-04-26
  • 2015-10-25
  • 1970-01-01
相关资源
最近更新 更多