java - 执行处理器“org.thymeleaf.spring5.processor.SpringOptionFieldTagProcessor”时出错
问题描述
我经常遇到下面列出的错误。
模型应该为表单提供 3 个 List<Food/Drink/Trick> 每个 Food,Drink,Trick 是一个单独的类。它是从数据库中获取的并且可以工作。
视图应列出每个 Food、Drink、Trick 属性名称的名称。
食物和饮料是单选的。技巧是多选。无论选择什么都应该传递给创建的对象。
我的麻烦在于 th:field="*{tricks}"。
非常感谢您的回复。
public String createNewFoxGet(Model model, @RequestParam(required = false) String name) {
if (name == null) {
name = "";
}
model.addAttribute("fox", new Fox());
model.addAttribute("tricksAll", trickService.getAllTrickTypes());
model.addAttribute("drinkAll", drinkService.getAllDrinkTypes());
model.addAttribute("foodAll", foodService.getAllFoodTypes());
model.addAttribute("name", name);
return "createnewfox";
}
-----------------
<form action="#" th:action="@{/createnewfox}" th:object="${fox}" method="post">
<p>New Fox</p>
<div class="form-group-name">
<label for="name">Name:</label>
<input type="text" name="name" id="name" placeholder="match with signup name" th:field="*{name}" minlength="2">
</div>
<label for="favouriteFood">Favourite food:</label>
<select name="favouriteFood" id="favouriteFood">
<option th:each="element:${foodAll}" th:value="${element}" th:field="*{food}"
th:text="${element.getName()}"></option>
</select>
<label for="favouriteDrink">Favourite drink:</label>
<select name="favouriteDrink" id="favouriteDrink">
<option th:each="element:${drinkAll}" th:value="${element}" th:field="*{drink}"
th:text="${element.getName()}"></option>
</select>
<br>
<label for="foxTricks">Fox tricks:</label>
<select name="foxTricks" id="foxTricks" multiple>
<option th:each="element:${tricksAll}" th:value="${element}" th:field="*{tricks}"
th:text="${element.getName()}"></option>
</select>
<br>
<p>Hold down the Ctrl (windows) / Command (Mac) button to select multiple tricks.</p>
<br>
<button type="submit" class="btn btn-primary">GO!</button>
</form>
-----------------------------
An error happened during template parsing (template: "class path resource [templates/createnewfox.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/createnewfox.html]")
Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringOptionFieldTagProcessor' (template: "createnewfox" - line 47, col 69)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
... 48 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringOptionFieldTagProcessor' (template: "createnewfox" - line 47, col 69)
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Long] for value 'Trick(id=1'; nested exception is java.lang.NumberFormatException: For input string: "Trick(id=1"```
解决方案
这个解决了。问题是 select 中的 name="foxTricks" 与实际的对象属性名称不匹配。
推荐阅读
- python - 用单独的值替换每个匹配项
- c - 为什么我的 levenshtein 距离计算器无法显示 PDF 文件?
- kubernetes - GCP 中的 HELM 部署:尝试进行入口转发规则查找以在 hpa 中使用
- r - 我可以旋转由 igraph::graph 制作的网络图吗?
- javascript - react中的多表单什么都没有显示
- jquery - 使用 Laravel 在 ROUTE 中添加额外参数的 Ajax GET 请求
- python - Python中的Google Sheets API,创建电子表格然后编辑,找不到电子表格
- gsap - ScrollMagic 中有没有办法使 setPin 持续时间等于时间而不是滚动距离?
- php - CKFinder 3 - 浏览器为空白
- reverse-proxy - URL 路由(而不是子域路由)不起作用