我用一个输入文本和一个 textarea
创建了表单。输入文本工作正常,但 textarea 甚至不显示:
<div id="news" th:fragment="admin_panel">
<form method="POST" th:action="@{/addNews}" th:object="${news}" id="myform">
Tytuł:
<input type="text" th:field="*{title}"/>
<input type="submit" value="Wstaw"/>
</form>
<textarea name="news_content" rows="20" cols="80" th:field="${news.content}" form="myform">
...
</textarea>
</div>
When I delete the th:field
the textarea
is displayed and when I use th:value
instead of th:field
it’s displayed too but doesn’t save the将文本写入 news.content(news.title 保存正常)。
我不知道……我读了 thymeleaf 参考资料但找不到答案,所以请帮助好人!
原文由 Bambelal 发布,翻译遵循 CC BY-SA 4.0 许可协议
您必须使用选定的对象表达式
*{content}
并将 textarea 标签放在 form 标签内!最后,所有关于生成的
name
属性的结果形式。该名称需要对应于所选根对象中的propertyAccessor
th:object
。表单由 spring 处理(没有任何 thymeleaf 拦截)。关于 spring 集成的文档非常好: http ://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html
他们这样说:
th:field 属性的值必须是选择表达式 (*{…}),这是有道理的,因为它们将在表单支持 bean 而不是上下文变量(或 Spring MVC 术语中的模型属性)上进行评估).
编辑:感谢项目链接,修复很简单: