【发布时间】:2022-01-20 19:15:57
【问题描述】:
第一个被剪断的代码是我添加(或者更确切地说是尝试)在 html 中使用的属性“NewUser”。
'th:field="*{name}"' 弹出错误。
错误: 执行处理器“org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor”时出错
@RequestMapping("home")
public String newUser(Model model) {
User newUser = new User();
newUser.setId(100l);
newUser.setName("Test");
newUser.setEmail("test@what.com");
newUser.setPermission(0);
model.addAttribute(newUser);
return "index";
}
<form action="#" th:action="@{/home/add}" th:object="${newUser}" method="get">
<p>Id: <input type="text" th:field="*{name}" /></p>
<p>Message: <input type="text" th:field="*{email}" /></p>
<p>Message: <input type="text" th:field="*{permission}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
【问题讨论】:
-
你可以尝试使用 th:object="${user}" 我相信当 Model.addAttribute 被调用时没有 attributeName 参数它使用小写类的名称
-
遗憾的是它没有改变任何东西,同样的错误同一行。
-
我同意 Josh Van de Walle 的观点。 Thymeleaf 找不到那个对象。要么你没有正确命名它(例如你应该这样做
model.addAttribute("newUser", newUser);而不是model.addAttribute(newUser);)或者你在错误的控制器上并且根本没有添加对象。 See this (my code is the same as yours except I'm naming my attribute).
标签: spring-boot thymeleaf